Shell脚本实现在Linux系统中自动安装JDK
程序员文章站
2022-06-07 22:57:06
a:本脚本运行的机器,linux
b:待安装jdk的机器, linux
首先在脚本运行的机器a上确定可以ssh无密码登录到待安装jdk的机器b上,然后就可以在a上运行本...
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=?,保证可以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="ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz"
# do not change below two lines:
install_dir="/usr/local/lib/java"
local_dir="./.tmp"
function install_jdk() {
echo -e "<info> install jdk on machine: $1"
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")
local 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_error
fi
echo -e "<info> create folder on: $dest_login:$install_dir"
local ret=`ssh $dest_login "mkdir $install_dir"`
echo -e "<info> copy $jdk_home/ to: $dest_login:$install_dir/"
local ret=`scp -r $local_dir/jdk_untar/$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/profile"`
local ret=`ssh $dest_login "echo '#!{{install_jdk@hgdb.net==>' >> /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 path=\\$java_home/bin:\\$java_home/jre/bin:\\$path' >> /etc/profile"`
local ret=`ssh $dest_login "echo '#!<==install_jdk@hgdb.net}}'>> /etc/profile"`
local ret=`ssh $dest_login ". /etc/profile"`
}
function uninstall_jdk() {
echo -e "<todo> uninstall jdk from: $1"
}
#=======================================================================
# ---- main() ----
if [ -n $1 ]; 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 "root@$dest_ip" "$jdk_src"
fi
上一篇: 想要做好SEO是否还需要坚持写原创文章