Now we will demonstrate how to use Bash scripts to automatically deploy JDK.
Environment: ubuntu12.04 amd 64 desktop or server, which must be BASH Shell
Copy the directory Java to the target machine and run the install. Sh script with the root permission.
Take a look at the directory structure:
. ── Check. Sh ├ ── install. Sh ├ ── jdk1.6.0 _ 35 ── readme ── tool. Sh
Install. sh. This script will first call check. the function in the sh script checks whether JDK is installed and whether the version number is 1.6.0 _ 35. if no installation is available or the version number is different, install it.
The JDK 1.6.0 _ 35 directory is the Linux 64-bit JDK package of oralce after decompression.
Tool. Sh provides some basic functions for copying directories and creating soft links.
First check the install. Sh script:
#! /Bin/bash source check. shsource tool. shhasjavar =$? Src_jdk_files =. /jdk1.6.0 _ records =/usr/jdk1.6.0 _ 35if [$ R-EQ 1] Then ECHO "Java was installed" else echo "Java was not installed" copyfolder $ src_jdk_files $ export CreateLink $ dst_jdk_files/usr/jdk1.6 CreateLink '/usr/jdk1.6/bin/Java ''/etc/alternatives/Java 'createlink/etc/alternatives/Java/usr/bin/javafi
Check. Sh script again
#! /Bin/bash # Check Java is available in this sessionfunction hasjava {v = 'java-Version 2> & 1 'echo 'Check Java platform... 'pos = 'expr match "$ v" 'java version \ "1.6.0 _ 35 \" ''if [$ pos-GT 0] thenreturn 1 elsereturn 0 fi}
Finally, let's look at the tool. Sh script.
#! /Bin/bashfunction copyfolder {if [-d "$2"] thenecho "$2 folder exists already, stop copying. "elseecho" $2 folder doesn't exists, Start copying... "CP-r $1 $2 fi} # create a link ($2) to file ($1) function CreateLink {if [-F "$2"] thenecho "$2 link exists already, removing it... "RM $2 elseecho" $2 link doesn't exists, creating it... "fi echo" creating link: $2 to $1 "ln-S $1 $2}
With this, it will be much easier to work in the future.