环境:centos7
请参考https://www.cnblogs.com/maohai-kdg/p/13474024.html
哈哈,感觉写的比之前好多了。纯SHELL小白,考虑的不深,如有错误的地方请各位大神不吝赐教,谢谢!
#!/bin/bash
#退出脚本
function exit_script(){
exit 1
}
function caozuo(){
systemctl start docker
systemctl enable docker
docker version
}
var=$(uname -r)
temp=${var:0:4}
a=$(echo $temp)
b=3.10
a1=${a:0:1}
a2=${a:2:1}
a3=${a:3:1}
b1=${b:0:1}
b2=${b:2:1}
b3=${b:3:1}
temp1=`expr $a1 * 100 - $b1 * 100 + $a2 * 10 - $b2 * 10 + $a3 - $b3`
#echo $temp1
echo "当前版本为$a"
if [ $temp1 -ge 0 ]
then
echo "可以安装docker"
else
echo "版本过低,安装docker至少需要3.10以上内核版本,暂时无法安装docker"
exit_script
fi
echo "开始安装docker"
#卸载旧版本
yum remove -y docker docker-common docker-selinux docker-engine
#安装依赖的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
#设置yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates |sort -r
function xuanze(){
echo "如您需要选择特定版本请输入Y/y,如您选择N/n,则默认安装最新版本"
stty erase '^H'
read -p "please choise Y/N: " answer
case $answer in
Y|y|yes|YES)
echo "您选择安装特定版本,格式如下:18.06.3"
read -p "请输入您选择的版本: " banben;;
N|n|no|NO) echo "Now,installing the new..."
yum -y install docker
caozuo
exit_script ;;
*) echo "sorry,your input error,please repeat it"
xuanze ;;
esac
}
xuanze
#echo $banben
#echo $answer
tempfile=$(mktemp testinstalldocker.XXXXXX)
yum list docker-ce --showduplicates |sort -r > $tempfile
#echo $(grep -c $banben $tempfile )
te1=docker-ce-
te2=.ce
ver=$te1$banben$te2
if [ `grep -c $banben $tempfile` -eq 1 ]
then
echo "your select version is $ver"
echo "Now,installing docker..."
else
echo "Sorry,your are not select right,please repeat it"
exit_script
fi
rm -rf $tempfile
yum -y install $ver
caozuo
版本更新信息:2020/09/08添加Y/N验证信息
后续继续改进!!!