乐趣区

关于shell:我写了一个简单的shell脚本用于再CentOS-7服务器上快速安装JDK

我写了一个简略的 shell 脚本用于再 CentOS 7 服务器上疾速装置 JDK,其余版本的零碎没有试过
废话不多说间接上代码
用的是华为的 JDK 镜像

#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Color
red='\033[31m'
green='\033[32m'
yellow='\033[33m'
plain='\033[0m'

get_char() {SAVEDSTTY=$(stty -g)
  stty -echo
  stty cbreak
  dd if=/dev/tty bs=1 count=1 2>/dev/null
  stty -raw
  stty echo
  stty $SAVEDSTTY
}
# 一些阐明
clear
echo
echo -e "${yellow}============================================================${plain}"
echo 'System Required: CentOS 7'
echo 'Description: Install JDK1.8'
echo 'Version: 1.0.0'
echo 'Author: Jonsson <yz808@outlook.com>'
echo 'Blog: https://blog.csdn.net/y1534414425'
echo 'GitHub: https://github.com/jonssonyan'
echo -e "${yellow}============================================================${plain}"
echo
echo "Press any key to start...or Press Ctrl+C to cancel"
char=$(get_char)
# 判断是否为 root 用户
[[$EUID -ne 0]] && echo -e "[${red}Error${plain}] This script must be run as root!" && exit 1
# 判断 JDK 源文件是否下载过
if [! -f "./jdk-8u202-linux-x64.tar.gz"]; then
  wget https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
fi
# 解压 JDK 到指定文件夹
mkdir -p /usr/local/java && tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/local/java
# 增加 JDK 到环境变量
chmod 700 /etc/profile
echo '#Java Env' >>/etc/profile
echo 'export JAVA_HOME=/usr/local/java/jdk1.8.0_202' >>/etc/profile
echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >>/etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile
source /etc/profile
# 判断是否为 32 位零碎
bit=$(getconf LONG_BIT)
if [${bit} -eq 32 ]; then
  sudo yum install glibc.i686
fi
# 检测 Java 是否装置
java -version
if [$? -eq 0]; then
  echo -e "${yellow}============================================================${plain}"
  echo -e "${green}JDK1.8 装置胜利 ${plain}"
  echo -e "${yellow}============================================================${plain}"
else
  echo -e "${yellow}============================================================${plain}"
  echo -e "${red}JDK1.8 装置失败, 请查看你的配置 ${plain}"
  echo -e "${yellow}============================================================${plain}"
fi
退出移动版