【shell 练习1】编写Shell条件句练习

时间:2023-03-10 01:35:48
【shell 练习1】编写Shell条件句练习

实例一、比较两个整数大小

#!/bin/bash
while true
do
read -p "Please input two int nums:" a b
expr $a + $b + >/dev/null >&1 #判断是否为整数
if [ $? -eq ];then #返回值是否为0
if [ $a -gt $b ];then
echo " $a > $b"
exit
elif [ $a -lt $b ];then
echo " $a < $b "
exit
else [ $a -eq $b ]
echo " $a = $b "
exit
fi
fi
done

实例二 、测试语句和if条件句混合使用,通过目录安装lamp,lnmp

目录安装:
#!/bin/sh
path=/server/scrips
[ ! -d "$path" ] && mkdir $path -p
#menu
cat <<END
.[install lamp]
.[install lnmp]
.[exit]
pls input the num you wang:
END
read num
expr $num + &>/dev/null
if [ $? -ne ];then
echo "the num you input must be {1|2|3}"
exit
fi
[ $num -eq ]&&{
echo "start install lamp."
sleep ;
[ -x "$path/lamp.sh" ]||{
echo " does not exist or can not be exec."
exit
}
$path/lamp.sh
#source $path/lamp.sh
exit $?
}
if [ $num -eq ];then
echo "start install lnmp."
sleep ;
if [ -x "$path/lamp.sh" ];then
$path/lnmp.sh
#source $path/lamp.sh
exit $?
else
echo " does not exist or can not be exec. "
exit
fi
fi
if [ $num -eq ];then
echo bye
exit
fi
[[ ! $num =~[-] ]] &&{
echo "the num you input must be {1|2|3}"
echo "Input ERROR"
exit
}