安装jdk1.8
这是很普通的shell的脚本,只需要注意:echo 语句内出现的变量,需要转义!
#!/bin/bash
function get_current_time_stamp()
{
echo `date "+%Y/%m/%d %H:%M:%S"`
}
function send_error()
{
echo -e "\e[1;45m [ Error ] `get_current_time_stamp` - $1 -\e[0m"
}
function send_success()
{
echo -e "\e[1;32m [ Success ] `get_current_time_stamp` - $1 -\e[0m"
}
function send_info()
{
echo -e "\e[1;34m [ Info ] `get_current_time_stamp` - $1 -\e[0m"
}
function send_warn()
{
echo -e "\e[1;33m [ Warn ] `get_current_time_stamp` - $1 -\e[0m"
}
check_java=$(rpm -qa|grep -E "^java" | wc -l)
if [ $check_java -gt 0 ];then
send_error "java has been installed, exit..."
exit 1
fi
if [ ! -d /usr/local ];then
mkdir -p /usr/local
chmod 755 /usr/local
fi
if [ ! -e "./jdk-8u181-linux-x64.tar.gz" ];then
send_error "jdk package not found..."
exit 1
fi
tar -zxvf ./jdk-8u181-linux-x64.tar.gz -C /usr/local/
if [ $? -ne 0 ];then
send_error "tar error, Please Check!"
exit 1
fi
cp /etc/profile /etc/profile.bak
echo "JAVA_HOME=/usr/local/jdk1.8.0_181" >> /etc/profile
echo "JRE_HOME=\$JAVA_HOME/jre" >> /etc/profile
echo "PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
echo "CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
echo "export JAVA_HOME" >> /etc/profile
echo "export JRE_HOME" >> /etc/profile
echo "export PATH" >> /etc/profile
echo "export CLASSPATH" >> /etc/profile
send_info "JDK install finished."