一、docker安装脚本
创建
/server/scripts/shell/install_docker.sh
function redecho() {
local str=”$*”
echo -e “\E[1;31m${str}\E[0m”
return $?
}
function greenecho() {
local str=”$*”
echo -e “\E[1;32m${str}\E[0m”
return $?
}
function yellowecho() {
local str=”$*”
echo -e “\E[1;33m${str}\E[0m”
return $?
}
function blueecho() {
local str=”$*”
echo -e “\E[1;34m${str}\E[0m”
return $?
}
#1.vars
file=~/docker-27.0.3.tgz
systemctl_file=~/docker.service
#2.check_root
function check_root(){
if [ $UID -ne 0 ];then
redecho “使用root运行脚本”
exit 1
fi
return $?
}
#3.check_files
function check_files(){
if [ ! -f $file -o ! -f $systemctl_file ];then
redecho “$file 文件或$systemctl_file文件不存在”
exit 2
fi
return $?
}
#4.解压与修改所有者
function tar_docker(){
tar xf $file >/dev/null 2>&1
if [ $? -eq 0 ];then
greenecho “$file 解压成功”
rc=0
else
redecho “$file 解压失败”
exit 3
rc=1
fi
chown -R root:root docker
return $rc
}
#5.insatll docker
function install_docker(){
mv docker/* /bin/
if [ $? -eq 0 ];then
greenecho “$file 移动成功”
rc=0
else
redecho “$file 移动失败”
exit 4
rc=0
fi
return $?
}
#6.config docker
function config_docker(){
mv $systemctl_file /usr/lib/systemd/system/
mkdir -p /etc/docker
cat >/etc/docker/daemon.json<<‘EOF’
{
“registry-mirrors” : [“https://do.nark.eu.org”,
“https://dc.j8.work”,
“https://docker.m.daocloud.io”,
“https://dockerproxy.com”,
“https://docker.mirrors.ustc.edu.cn”,
“https://docker.nju.edu.cn”,
“https://docker.mirrors.ustc.edu.cn”,
“https://docker.registry.cyou”,
“https://docker-cf.registry.cyou”,
“https://dockercf.jsdelivr.fyi”,
“https://docker.jsdelivr.fyi”,
“https://dockertest.jsdelivr.fyi”,
“https://mirror.aliyuncs.com”,
“https://dockerproxy.com”,
“https://mirror.baidubce.com”,
“https://docker.m.daocloud.io”,
“https://docker.nju.edu.cn”,
“https://docker.mirrors.sjtug.sjtu.edu.cn”,
“https://docker.mirrors.ustc.edu.cn”,
“https://mirror.iscas.ac.cn”,
“https://docker.rainbond.cc”,
“https://do.nark.eu.org”,
“https://docker.shootchat.top”,
“https://registry.docker-cn.com”]
}
EOF
return $?
}
#7.cinfig_kernel
function config_kernel(){
forward_count=
if [ $forward_count -eq 0 ];then
echo ‘net.ipv4.ip_forward = 1’ >>/etc/sysctl.conf
sysctl -p
#$?
else
sed -i ‘s#net.ipv4.ip_forward *= *o#net.ipv4.ip_forward = 1#g’ /etc/sysctl.conf
sysctl -p
#$?
fi
return $?
}
#8.start_enable docker
function start_docker(){
systemctl daemon-reload
systemctl enable –now docker
if [ $? -eq 0 ];then
greenecho “$file 启动成功”
rc=0
else
redecho “$file 启动失败”
exit 5
rc=1
fi
return $rc
}
#9.main
function main(){
check_root
check_files
tar_docker
install_docker
config_docker
config_kernel
start_docker
}
main
/server/scripts/shell/install_docker.sh
function redecho() {
local str=”$*”
echo -e “\E[1;31m${str}\E[0m”
return $?
}
function greenecho() {
local str=”$*”
echo -e “\E[1;32m${str}\E[0m”
return $?
}
function yellowecho() {
local str=”$*”
echo -e “\E[1;33m${str}\E[0m”
return $?
}
function blueecho() {
local str=”$*”
echo -e “\E[1;34m${str}\E[0m”
return $?
}
#1.vars
file=~/docker-27.0.3.tgz
systemctl_file=~/docker.service
#2.check_root
function check_root(){
if [ $UID -ne 0 ];then
redecho “使用root运行脚本”
exit 1
fi
return $?
}
#3.check_files
function check_files(){
if [ ! -f $file -o ! -f $systemctl_file ];then
redecho “$file 文件或$systemctl_file文件不存在”
exit 2
fi
return $?
}
#4.解压与修改所有者
function tar_docker(){
tar xf $file >/dev/null 2>&1
if [ $? -eq 0 ];then
greenecho “$file 解压成功”
rc=0
else
redecho “$file 解压失败”
exit 3
rc=1
fi
chown -R root:root docker
return $rc
}
#5.insatll docker
function install_docker(){
mv docker/* /bin/
if [ $? -eq 0 ];then
greenecho “$file 移动成功”
rc=0
else
redecho “$file 移动失败”
exit 4
rc=0
fi
return $?
}
#6.config docker
function config_docker(){
mv $systemctl_file /usr/lib/systemd/system/
mkdir -p /etc/docker
cat >/etc/docker/daemon.json<<‘EOF’
{
“registry-mirrors” : [“https://do.nark.eu.org”,
“https://dc.j8.work”,
“https://docker.m.daocloud.io”,
“https://dockerproxy.com”,
“https://docker.mirrors.ustc.edu.cn”,
“https://docker.nju.edu.cn”,
“https://docker.mirrors.ustc.edu.cn”,
“https://docker.registry.cyou”,
“https://docker-cf.registry.cyou”,
“https://dockercf.jsdelivr.fyi”,
“https://docker.jsdelivr.fyi”,
“https://dockertest.jsdelivr.fyi”,
“https://mirror.aliyuncs.com”,
“https://dockerproxy.com”,
“https://mirror.baidubce.com”,
“https://docker.m.daocloud.io”,
“https://docker.nju.edu.cn”,
“https://docker.mirrors.sjtug.sjtu.edu.cn”,
“https://docker.mirrors.ustc.edu.cn”,
“https://mirror.iscas.ac.cn”,
“https://docker.rainbond.cc”,
“https://do.nark.eu.org”,
“https://docker.shootchat.top”,
“https://registry.docker-cn.com”]
}
EOF
return $?
}
#7.cinfig_kernel
function config_kernel(){
forward_count=
grep ip_forward /etc/sysctl.conf | wc -lif [ $forward_count -eq 0 ];then
echo ‘net.ipv4.ip_forward = 1’ >>/etc/sysctl.conf
sysctl -p
#$?
else
sed -i ‘s#net.ipv4.ip_forward *= *o#net.ipv4.ip_forward = 1#g’ /etc/sysctl.conf
sysctl -p
#$?
fi
return $?
}
#8.start_enable docker
function start_docker(){
systemctl daemon-reload
systemctl enable –now docker
if [ $? -eq 0 ];then
greenecho “$file 启动成功”
rc=0
else
redecho “$file 启动失败”
exit 5
rc=1
fi
return $rc
}
#9.main
function main(){
check_root
check_files
tar_docker
install_docker
config_docker
config_kernel
start_docker
}
main
二、Shell编程总结
- 变量:
- 普通变量:命名,金庸新作
- 特殊环境变量:
$n $0 $# $* $@ $?
在脚本和函数中区别
在脚本和函数中区别
- 环境变量:PATH,LANG,PS1,UID
- 判断
- test/[] 条件测试语句
- 判断文件:-f,-d,x,-h/-L
- 大小:-eq,-ne,gt,ge,lt,le
- 字符串:= != -z -n
- 逻辑:-a -o !
- 正则:[[ =~ 正则]]
- if
- 单分支
- 双分支
- 多分支
- case
- 菜单选择
- 判断系统
- 服务管理脚本
- test/[] 条件测试语句
- 循环
- for
- while
- 死循环
- 读取文件内容
- 循环控制
- break
- continue
- exit
- return
- 计算:let,awk,bc
- 函数:注意格式与命名,函数传参
- 颜色,函数库
- 数组
- debug调试代码
- bash/sh -x 显示过程
- set -x 开始详细 ,set +x结束
- 目标:
- 巡检脚本
- 服务部署脚本
- docker
- mysql
- tomcat
- nginx(编译)
- openssh(编译)
- 阅读脚本