安装脚本算是shell最常见的一个用途之一了
下面是一个demo
#!/bin/bash
# date 2024/8/21
hostpath=/root/interview/install
#检测目录是否存在,不存在创建它
[ ! -d $hostpath ] && mkdir $hostpath -p
cat <<END
1.[ install lamp ]
2.[ install lnmp ]
3.[ exit ]
pls input the number you want:
END
read num
# 根据输入的数字做选择
expr $num + 1 &> /dev/null
[ $? -ne 0 ] && {
echo number you input must be {1|2|3}
}
# 根据num做逻辑判断
[ $num -eq 1 ] && {
echo "正在安装lamp。。。"
sleep 2
[ -x $hostpath/lamp.sh ] || {
echo "the file does not exist or cant be exec "
exit 1
}
}
[ $num -eq 2 ] && {
echo "正在安装lnmp。。。"
sleep 2
[ -x $hostpath/lnmp.sh ] || {
echo "the file does not exist or cant be exec "
exit 1
}
}
[ $num -eq 3 ] && {
exit 0
}
亮点是格式比较正规
参考来源:
https://www.bilibili.com/video/BV1d34y1j763?p=40&vd_source=6dc445c1be9dad41cefb811dae015c1b