首页 > 代码库 > Linux自动安装JDK的shell脚本

Linux自动安装JDK的shell脚本

Linux自动安装JDK的shell脚本


A:本脚本运行的机器,Linux

B:待安装JDK的机器, Linux


首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在A上运行本脚本:

$ ./install-jdk.sh B的IP

or:

$ ./install-jdk.sh "B的IP" "JDK的URI"

就可以在机器B上安装JDK。jdk使用的tar包需要用户自己设定DEFAULT_JDK_SRC=http://www.mamicode.com/?,保证可以wget得到即可。下面是全部脚本内容:

#!/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 - root@ipaddr
#   jdkUri  - uri for fetching tarball
#
# Example:
#
#   install_jdk root@192.168.122.206 ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz
#
#***********************************************************
. common.sh

# YOU MIGHT CHANGE BELOW LINE TO GET YOUR JDK TARBALL:
DEFAULT_JDK_SRC=http://www.mamicode.com/"ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz">
此外使用到了common.sh这个脚本:

#!/bin/bash
#
# @file
#   common.sh
#
# @date
#   2013-12-18
#
# @author
#   cheungmine
#
# @version
#   0.0.1pre
#
########################################################################
# Error Codes:
#
ERR_NOERROR=0

ERR_FATAL_ERROR=-100

# current user is not a root
ERR_NOT_ROOT=100

# file is already existed
ERR_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 root.\n"             "   Because you will run all the steps from this shell with root"             "privileges,\n"             "   you 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=http://www.mamicode.com/$(read_cfg $CFG_FILE $SEC_NAME ‘key‘)>


Linux自动安装JDK的shell脚本