这是学习shell脚本的第三天
这是一个安装卸载redis的脚本
#! /bin/bash
function redisInstall(){
echo "
----------------redis-----------------
1、Redis Version
2、Redis Install&start
3、Redis Stop&Remove
4、Redis Start
5、Redis Stop
6、Redis Enable
7、Redis Disable
8、Redis Status
9、Exit
0、Menu
"
while true
do
read -p "input info:" char
case ${char} in
1)
#
if ! test -z "$(find /usr/local/bin/ -name redis-server)" || ! test -z "$(find /usr/bin/ -name redis-server)" ;then
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
redis-server -v
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
else
echo "Exception: Version information does not exist!"
fi
;;
2)
yum install -y redis && systemctl start redis
;;
3)
read -p "Are you sure you want to uninstall redis???(yes/no | y/n)": yesOrNo
if [ "${yesOrNo}" = "yes" -o "${yesOrNo}" = "y" ];
then
i=0
while true;do
sleep 0.1
let i++
if [ "${i}" -eq 80 ];then
break
fi
echo -n "."
done
echo "start Uninstall!!!"
systemctl stop redis && yum remove -y redis && rm -rf /var/lib/redis /var/log/redis /usr/local/bin/redis-server /usr/bin/redis-server && echo "Uninstall complete!" || echo "Exception: Service does not exist!"
elif [ "${yesOrNo}" = "no" -o "${yesOrNo}" = "n" ];
then
:<<EOF i=0
while true;do
sleep 0.6
let i++
if [ "${i}" -eq 5 ];then
break
fi
echo -n "."
done
sh /app/nas/redisStatusInfo.sh
else
EOF
sh /app/nas/redisStatusInfo.sh
fi
;;
4)
systemctl start redis || echo "Exception: Service does not exist!"
;;
5)
systemctl stop redis || echo "Exception: The service is not started or does not exist!"
;;
6)
systemctl enable redis || echo "Exception: The service does not exist or is abnormal!"
;;
7)
systemctl disable redis || echo "Exception: The service does not exist or is abnormal!"
;;
8)
systemctl status redis || echo "Exception: The service is not started or does not exist!"
;;
9)
exit
;;
0)
sh /app/nas/redisStatusInfo.sh
;;
*)
if [ ! -z "${char}" ];then
echo "warning!"
fi
esac
done
}
redisInstall
注释:
#! /bin/bash
function redisInstall(){ #菜单 ?echo " ? ? ?----------------redis----------------- ? ? ?1、Redis Version ? ? ?2、Redis Install&start ? ? ?3、Redis Stop&Remove ? ? ?4、Redis Start ? ? ?5、Redis Stop ? ? ?6、Redis Enable ? ? ?7、Redis Disable ? ? ?8、Redis Status ? ? ?9、Exit ? ? ?0、Menu ? ? " #定义死循环 ? ? ?while true? ?do #输入 ? ? read -p "input info:" char ? ? case ${char} in #分支语句 ? ? 1) ? ? ? #查找并判断是否存在redis-server ? ? ? if ! test -z "$(find / -name redis-server)" ;then ? ? ? echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ? ? ? redis-server -v#打印版本信息 ? ? ? echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ? ? ? else#异常 ? ? ? echo "Exception: Version information does not exist!" ? ? ? fi ? ? ;; ? ? 2)#安装&启动&开启启动 ? ? ? yum install -y redis && systemctl start redis && systemctl enable redis ? ? ;; ? ? 3)#输入 询问是否删除? ? ? ? read -p "Are you sure you want to uninstall redis???(yes/no | y/n)": yesOrNo ? ? ? if [ "${yesOrNo}" = ?"yes" -o "${yesOrNo}" = ?"y" ];#判断输入yes还是y ? ? ? ?then ? ? ? ? i=0 ? ? ? while true;do #定义死循环 ? ? ? ?sleep 0.1 #0.1毫秒 ? ? ? ?let i++ #每0.1毫秒增加一个.点 ? ? ? ?if [ "${i}" -eq 80 ];then #增加到第80个点时终止循环 ? ? ? ? ?break #终止循环 ? ? ? ?fi ? ? ? ? ?echo -n "." #输出.点 ? ? ? done ? ? ? echo "start Uninstall!!!" #开始卸载 ? ? ? #关闭&卸载&删除残留文件 ? ? ? systemctl stop redis && yum remove -y redis && rm -rf /var/lib/redis /var/log/redis /usr/local/bin/redis-server /usr/bin/redis-server && echo "Uninstall complete!" || echo "Exception: Service does not exist!" ? ? ? elif [ "${yesOrNo}" = ?"no" -o "${yesOrNo}" = ?"n" ];#判断输入no还是n ? ? ? ?then :<<EOF i=0 #注释 ? ? ? while true;do ? ? ? ?sleep 0.6 ? ? ? ?let i++ ? ? ? ?if [ "${i}" -eq 5 ];then ? ? ? ? ?break ? ? ? ?fi ? ? ? ? ?echo -n "." ? ? ? done ? ? ? sh /app/nas/redisStatusInfo.sh ? ? ? else EOF ? ? ? #卸载完毕回到菜单 ? ? ? sh ./redisStatusInfo.sh ? ? ? fi
? ? ;; ? ? 4)#启动redis ? ? ? systemctl start redis || echo "Exception: Service does not exist!" ? ? ;; ? ? 5)#关闭redis ? ? ? systemctl stop redis ?|| echo "Exception: The service is not started or does not exist!" ? ? ;; ? ? 6)#redis开机启动 ? ? ? systemctl enable redis || echo "Exception: The service does not exist or is abnormal!" ? ? ;; ? ? 7)#关闭开机启动 ? ? ? systemctl disable redis || echo "Exception: The service does not exist or is abnormal!" ? ? ;; ? ? 8)#查看redis状态 ? ? ? systemctl status redis || echo "Exception: The service is not started or does not exist!" ? ? ;;? ? ? 9) ? ? ? exit ? ? ;; ? ? 0)#回到主菜单 ? ? ? sh ./redisStatusInfo.sh ? ? ;; ? ? *) ? ? #输入内容不在分支选项内 ? ? if [ ! -z "${char}" ];then ? ? echo "warning!" ? ? fi
? ? esac done
}
#执行方法 redisInstall ?
|