shell文件测试,菜单表示思想
Posted tdtdttdd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell文件测试,菜单表示思想相关的知识,希望对你有一定的参考价值。
---恢复内容开始---
文件测试表达式
-f 文件存在且为普通文件
-d 文件存在且为目录文件
-s 文件大小不为0则真
-e 文件存在则真
-r 文件存且可读 #文件属性对于root有时不好用
-w 写
-x 执行
-L 存在且为链接文件
f1 -nt f2 f1比f2新则为真
f1 -ot f2 f1比f2旧则为真
这些操作符可用于[[]],[],test几乎通用,更多操作符查看man test.
生产事例:
prog="rpcbind"
[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog #如果存在则加载
[ -f /etc/sysconfig/$prog ] || exit 1 #不存在退出
事例2,大括号可执行多个命令
[ $q -eq 3 ]&&{
echo 1
echo 2
echo 3
}
判断是否为整数:去掉“非数字”是否等于本身 ["`echo "123a|sed -r ‘s#[^0-9]##g‘`" = "123a"]
#!/bin/sh
read -p "pls input two num :" num1 num2
a=num1
b=num2
#no.1
[ $# -ne 2 ]&&{ #"$#"表示输入的变量个数
echo "usage: num1 num2"
exit 1
}
#no.2
["`echo "$a"|sed -r ‘s#[^0-9]##g‘`" = "$a" ]||{
echo "first arg must be int"
exit 2
}
#no.3
[ $1 -lt $b ]&&{
echo "$a<$b"
exit 3
}
企业综合事例:1.当用户输入1时,输出“start install lamp。” 然后执行/server/script/lamp.sh,脚本内容输出 “lamp is install”后退出脚本
2。当用户输出2时,输出“start install lnmp。” 然后执行/server/script/lnmp.sh,脚本内容输出 “lnmp is install”后退出脚本
3.当输入3时,退出当前菜单及脚本
4.当输入任何其他自负,给出提示“input error”后 退出脚本
5.要对执行的脚本进行条件判断,例如:脚本是否存在,是否可执行等。
打印简单选择菜单示例1,
cat <<END
1.wangfei
2.gongli
END
read -p "which do you like?:" a
echo "i guess,you like $a"
打印简单选择菜单示例2,函数写法,
menu(){
cat <<END
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
END
}
menu
read num
[ "$num" = "1" ]&&{
echo "start install lamp."
[ -x /server/script/lamp.sh ]||{
echo "lamp.sh error"
exit 2}
. /server/script/lamp.sh
}
[ "$num" = "3" ]&&{
echo "bye"
exit 0
}
echo "Iput Error"
exit 1
打印多级菜单示例:
cat << EOF
|-----------System Infomation-----------
| DATE :$DATE
| HOSTNAME :$HOSTNAME
| USER :$USER
| IP :$IPADDR
| DISK_USED :$DISK_SDA
| CPU_AVERAGE:$cpu_uptime
----------------------------------------
|****Please Enter Your Choice:[0-5]****|
----------------------------------------
(1) Configure $SERVER1 Service
(2) Configure $SERVER2 Service
(3) Configure NETWORKE Service
(4) Change Passwd
(5) Logs
(0) Quit
EOF
read -p "Please enter your choice[0-5]: " input
case $input in
#web service
1)
clear
while [ "$flag" -eq 0 ]
do
cat << EOF
----------------------------------------
|****Please Enter Your Choice:[0-3]****|
----------------------------------------
(1) Start $SERVER1 Service
(2) Stop $SERVER1 Service
(3) ReStart $SERVER1 Service
(0) Back
EOF
read -p "Please enter your choice[0-3]: " input1
case $input1 in
1)
echo -e "\n>>>>>>>>>>>$DATE Start $SERVER1">>/log.txt
$SERVER1_START 2>>/log.txt
if [ $? == 0 ];then
echo "Start $SERVER1......................................................[OK]"
else
echo "Start $SERVER1......................................................[FAILED]"
fi
sleep 5
clear
;;
2)
echo -e "\n>>>>>>>>>>>$DATE Stop $SERVER1">>/log.txt
$SERVER1_STOP 2>>/log.txt
if [ $? == 0 ];then
echo "Stop $SERVER1.......................................................[OK]"
else
echo "Stop $SERVER1.......................................................[FAILED]"
fi
sleep 5
clear
;;
3)
echo -e "\n>>>>>>>>>>>$DATE ReStart $SERVER1">>/log.txt
/usr/local/tdoa/php/sbin/php-fpm restart 2>>/log.txt
$SERVER1_RESTART 2>>/log.txt
if [ $? == 0 ];then
echo "Restart $SERVER1....................................................[OK]"
else
echo "Restart $SERVER1....................................................[FAILED]"
fi
sleep 5
clear
;;
0)
clear
break
;;
*) echo "----------------------------------"
echo "| Warning!!! |"
echo "| Please Enter Right Choice! |"
echo "----------------------------------"
---恢复内容结束---
以上是关于shell文件测试,菜单表示思想的主要内容,如果未能解决你的问题,请参考以下文章