Linux Auto-install JDK shell script
A: This script runs the machine, Linux
B: The machine to install the JDK, Linux
First, on machine a where the script is running, you can log on to the machine B where the JDK is to be installed without SSH password, and then you can run this script on a:
$./install-jdk.sh B's IP
Or
$./install-jdk.sh "B's IP" "JDK uri"
You can install the JDK on machine B. The tar package used by the JDK requires the user to set the default_jdk_src= itself? , guarantee can be wget. Here are the full script contents:
#!/bin/bash## @file # install-jdk.sh## @date # 2013-12-19## @author # cheungmine## @version # 0.0.1pre## @usage: #./ Install-jdk.sh 192.168.122.206################################################################################# . common.sh#***********************************************************# install_jdk# install JDK on machine:/usr/ local/lib## parameters:# Machine-[email protected]# Jdkuri-uri for fetching tarball## example:## Install_j DK [email protected] ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz##********************************** *************************. common.sh# might change BELOW line to GET YOUR JDK tarball:default_jdk_src= "ftp://vm-ftp/pub/tarball/ Jdk-7u67-linux-x64.tar.gz "# Don't change BELOW-lines:install_dir="/usr/local/lib/java "local_dir="./.tmp " function Install_jdk () {echo-e "<INFO> install JDK on machine: $" local dest_login=$1 local jdk_uri=$2 Local tar=$ (basename $JDK _uri) echo-e "<info> JDK: ' $JDK _uri ' "Wget-c $JDK _uri-p $LOCAL _dir-o $LOCAL _dir/$TAR $ (is_empty_dir" $LOCAL _dir/jdk_untar ") Lo Cal ret=$? Case $ret in $DIR _not_existed) mkdir-p $LOCAL _dir/jdk_untar;; $DIR _is_empty);; $DIR _not_empty) rm-rf $LOCAL _dir/jdk_untar/*; *) exit $ERR _fatal_error;; ESAC # Untar to Jdk_untar tar-zxf $LOCAL _dir/$TAR-C $LOCAL _dir/jdk_untar $ (is_empty_dir "$LOCAL _dir/jdk_untar") Local ret=$? If ["$ret"-eq "$DIR _not_empty"]; Then local jdk_home= ' ls $LOCAL _dir/jdk_untar 2>/dev/null ' echo $jdk _home else exit $ERR _fatal_e Rror fi echo-e "<INFO> Create folder on: $DEST _login: $INSTALL _dir" local ret= ' ssh $DEST _login "mkdir $INST All_dir "' echo-e" <INFO> copy $jdk _home/to: $DEST _login: $INSTALL _dir/"local ret= ' Scp-r $LOCAL _dir/jdk_unta r/$jdk _home $DEST _login: $INSTALL _dir ' # Remove local tar rm-rf $LOCAL _dir/jdk_untar LocAl dest_java_home= $INSTALL _dir/$jdk _home echo-e "<TODO> remove old settings for INSTALL_JDK in/etc/profile" Echo-e "<INFO> set/etc/profile:java_home= $DEST _java_home" local ret= ' ssh $DEST _login "echo" >>/ETC/PR Ofile "' Local ret= ' ssh $DEST _login" Echo ' #! {{[email protected]==> ' >>/etc/profile ' ' local ret= ' ssh $DEST _login "echo ' Export java_home= $DEST _java_ HOME ' >>/etc/profile ' ' local ret= ' ssh $DEST _login "echo ' Export classpath=.:\ \ $JAVA _home/lib/tools.jar:\\ $JAVA _home/lib/dt.jar ' >>/etc/profile "' Local ret= ' ssh $DEST _login ' echo ' Export PA th=\\ $JAVA _home/bin:\\ $JAVA _home/jre/bin:\\ $PATH ' >>/etc/profile "' Local ret= ' ssh $DEST _login" echo "#!<[ Email protected]}} ' >>/etc/profile "' Local ret= ' ssh $DEST _login '. /etc/profile "'}function uninstall_jdk () {echo-e" <TODO> uninstall JDK from: $ "}#============================== =========================================#----Main ()----If [-N $]; Then dest_ip=$1 jdk_src= $DEFAULT _jdk_src if [$# = = 2]; Then jdk_src=$2 fi echo-e "<INFO> install JDK on ' $DEST _ip ', jdk: ' $JDK _src '" INSTALL_JDK "[EMAIL&N Bsp;protected] $DEST _ip "" $JDK _src "fi
In addition, the common.sh script is used:
#!/bin/bash## @file # common.sh## @date # 2013-12-18## @author # cheungmine## @version # 0.0.1pre##################### ##################################################### Error Codes: #ERR_NOERROR =0err_fatal_error=-100# Current User Is isn't a rooterr_not_root=100# file is already existederr_file_existed=200#****************************************** # chk_root# Check if is a root user## example:# chk_root#******************************************** function Chk_root () {if [$UID-ne 0]; then echo-e "<chk_root> Current user is not a roo t.\n "" Because you'll run all the steps from this shell with root "privileges,\n" "Must become root right by typing:\n" "$ sudo su" exit $ERR _not_root fi}#*************** # read_cfg# read section and key from INI file## example:# value=$ (read_cf G $CFG _file $SEC _name ' key ') #*******function Read_cfg () {cfgfile=$1 section=$2 keyname=$3 RET Val= ' awk-f ' = '/\[' $SECTION ' \]/{a=1}a==1&&$1~/' $KEYNAME '/{print $2;exit} ' $CFGFILE ' echo $RETVAL | Tr-d ' "'}#***********************************************************# write_cfg# write section and key to INI file## E xample:# $ (write_cfg $CFG _file ' secname ' key ' $value) #*********************************************************** function Write_cfg () {cfgfile=$1 section=$2 keyname=$3 newval=$4 retval= ' sed-i '/^\[$SECTION \]/,/^\[/{/^ \[$SECTION \]/b;/^\[/b;s/^ $KEYNAME *=.*/$KEYNAME = $NEWVAL/g;} "$CFGFILE ' echo $RETVAL}#***************************** # real_path# get real path of given relative path## example:# abs_path=$ (Real_path $ (dir Name $)) #***********************************************************function Real_path () {\CD "$"/bin/pwd}#***** **********************************************# copy_file# Copy source file to destigation file without overwriting## example:# copy_file $file 1 $file 2#**** function Copy_file () {src=$1 dst=$2 if [-a $DST] the n echo-e "<copy_file> file is already existed: ' $dst '" Exit $ERR _file_existed fi echo-e "<c Opy_file> ' $src ' =>\n ' $dst ' "dstdir=$ (dirname $dst) if [-D $dstdir] Then:else Mkdir-p $dstdir fi cp-p $src $dst}#***********************************************************# is_empty_dir# If a dir is empty# returns:# 0-not empty dir# 1-is empty dir# 2-dir not existed# example:# is_empty_d IR ~/workspace# echo $?## $ (is_empty_dir './' # echo $?#********************************************************** *dir_not_existed=2dir_is_empty=1dir_not_empty=0function Is_empty_dir () {local olddir= $PWD local indir=$1 if [! -D "$indir"]; Then Return $DIR _not_existed fi cd $indir local files= ' ls 2>/dev/null ' CD $olddir if [-N ' $files "]; Then return $DIR _not_empty else return $DIR _is_empty fi}
Linux Auto-install JDK shell script