Go Premium for a chance to win a PS4. Enter to Win


how could I change JAVA_HOME in all these files

Posted on 2014-09-22
Medium Priority
Last Modified: 2014-09-22
I am upgrading the JAVA_HOME parameter in a oracle application server.

I need change the string JAVA_HOME="/usr/java/jdk1.6.0_18" to JAVA_HOME="/usr/java/jdk1.7.0_45" in all of the following files.

Is there a way I can make a script to backup each file and update each file.


[forms@solaris middleware]$ find . -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;
[forms@solaris middleware]$
Question by:Jason Yu
  • 6
  • 4

Author Comment

by:Jason Yu
ID: 40337450
all files are under $MW_HOME which is /u2/oracle/middleware
LVL 68

Expert Comment

ID: 40337474
for file in $(find $MW_HOME -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;)
     sed -i.bak 's@JAVA_HOME="/usr/java/jdk1.6.0_18"@JAVA_HOME="/usr/java/jdk1.7.0_45"@' $file

The above will ceate a backup of each file (by appending ".bak") andthen change each file in place.

Author Comment

by:Jason Yu
ID: 40337506
Could you please explain a little bit of the "sed" line?

Also, do I need put these three four lines in a script and execute it?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

by:Jason Yu
ID: 40337514
Can I also add a log file to this script so that I can check the result?

LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 40337612

The "-i" flag tells "sed" to change the file "in place". If "-i" is immediately followed by some string then "sed" will create backup files using that string as a suffix.

The stuff between the quotes is the script to be interpreted by "sed". I used single quotes because there are double quotes in your patterns.

"s" is the subcommand ("subsitute") for replacing strings by other ones.

"@" is the field separator. I used it instead of the usual "/" because there are slashes in your patterns.

The string between the first and second separators is the one to be searched and replaced, and the string between the second and third separators is the one to replace the first string.

$file is the variable filled by the "for" loop with the results of the "find" command, one by one.

You should indeed create a file, maybe in your HOME directory or somewhere else, but not inside the directory structure you're working on (It might be that you choose the script's name as "something.sh", and the script obviously contains the search strings of "grep" and "sed"!) This file should contain the code I posted.
Execute the script either with

sh /path/to/script.sh

or with

chmod +x /path/to/script.sh

"sed -i" normally does not write any output to the terminal or to a log. We could write our own log, perhaps like this:

echo "------- $(date) -------" > $LOG
for file in $(find $MW_HOME -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;)
     echo "Now working on --- $file ---"
     sed -i.bak 's@JAVA_HOME="/usr/java/jdk1.6.0_18"@JAVA_HOME="/usr/java/jdk1.7.0_45"@' $file
     echo "Processed      --- $file --- with returncode $?"
   done >>$LOG 2>&1

I added "2>&1" to also redirect possible error messages to the new log, not just our own "echos".
LVL 21

Expert Comment

ID: 40337642
So you don't  have to go through this again when you update Java, I suggest you change it to:

/usr/java/latest is a link to the latest Java package installed, and gets relinked whenever you update Java
LVL 68

Expert Comment

ID: 40337656
Good point, given that the "latest" version is indeed desired!

Author Comment

by:Jason Yu
ID: 40337700
Dear experts, thank you two for quick responds, I really appreciate your help.

I will run the script as you suggested.


For this case, I just can't use the symbolic link. As it was explained in oracle article (Doc ID 1309855.1):



There are two different approaches that can be used to achieve the required goal. Both will be described briefly:

A) Installing a new JDK home, and then renaming the existing JDK directory

Please avoid using symlinks as previously suggested in this note, as tools such as T2P (test to production) do require a FULL JDK directory structure, and might fail if a symbolic link is used.

Following this approach, the JDK directory location originally used during the installation will not change , and hence it has the minimal drawback that it might keep a versioned name as for example "$INSTALL_HOME/jdk_<version>" while in truth it contains a new version. You may need to follow the next approach once to edit direcories to a generic name, but then able to update regularly by swapping out the contents of the jdk directory. (See the "Tips" in Note 1492980.1)

B) Installing a new JDK home directory and replacing the JAVA_HOME environmental variable in *** all *** the scripts referring to it.

This is the preferred approach if you want to do a partial upgrade (affecting to specific domains only, or not affecting to nodemanager), but its difficulty is around on how to know which files do refer to a JDK location.

The initial list of files to change will be :

* $INSTALL_HOME/wlserver_10.3/common/bin/commEnv.sh
* $INSTALL_HOME/user_projects/domains/<mydomain>/bin/setDomainEnv.sh
* $INSTALL_HOME/wlserver_10.3/common/nodemanager/nodemanager.properties
* $INSTALL_HOME/utils/bsu/bsu.sh
* $INSTALL_HOME/utils/quickstart/quickstart.sh
* $INSTALL_HOME/utils/uninstall/uninstall.sh
even that a more exhaustive way to determine them, will be to run the following command:

find . -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;
Step by Step Actions

Author Comment

by:Jason Yu
ID: 40337744
Wonderful, I got the script running successfully.  does the return code "0" mean it's successful?

------- Mon Sep 22 13:52:31 PDT 2014 -------
Now working on --- /u2/oracle/middleware/utils/clone/clone.sh ---
Processed --- /u2/oracle/middleware/utils/clone/clone.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/clone/restore.sh ---
Processed --- /u2/oracle/middleware/utils/clone/restore.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/uninstall/uninstall.sh ---
Processed --- /u2/oracle/middleware/utils/uninstall/uninstall.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/bsu/bsu.sh ---
Processed --- /u2/oracle/middleware/utils/bsu/bsu.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/quickstart/quickstart.sh ---
Processed --- /u2/oracle/middleware/utils/quickstart/quickstart.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/bin/runProvisioningAntTasks.sh ---
Processed --- /u2/oracle/middleware/FormsHome/bin/runProvisioningAntTasks.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/sqldeveloper/ide/bin/launcher.sh ---
Processed --- /u2/oracle/middleware/FormsHome/sqldeveloper/ide/bin/launcher.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/oui/bin/runSSHSetup.sh ---
Processed --- /u2/oracle/middleware/FormsHome/oui/bin/runSSHSetup.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/sysman/deploy/agentdeploy/remoteif/resources/scripts/sshConnectivity.sh ---
Processed --- /u2/oracle/middleware/oracle_common/sysman/deploy/agentdeploy/remoteif/resources/scripts/sshConnectivity.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/modules/oracle.ovd_11.1.1/oimtemplates/extendadschema.sh ---
Processed --- /u2/oracle/middleware/oracle_common/modules/oracle.ovd_11.1.1/oimtemplates/extendadschema.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/SSLGenCA.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/SSLGenCA.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/WLS_SSL_Client_Config.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/WLS_SSL_Client_Config.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/pasteConfig.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/pasteConfig.sh --- with re
LVL 68

Expert Comment

ID: 40337794
Yes, returncode 0 means "OK". Congrats!

Author Closing Comment

by:Jason Yu
ID: 40337871
woolmilkporc is best, his quick solution helped me achieve an important milestone.

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question