【shell 练习1】编写Shell条件句练习
实例一、比较两个整数大小
#!/bin/bash while true do read -p "Please input two int nums:" a b expr $a + $b + 0 >/dev/null 2>&1 #判断是否为整数 if [ $? -eq 0 ];then #返回值是否为0 if [ $a -gt $b ];then echo " $a > $b" exit 0 elif [ $a -lt $b ];then echo " $a < $b " exit 0 else [ $a -eq $b ] echo " $a = $b " exit 0 fi fi done
实例二 、测试语句和if条件句混合使用,通过目录安装lamp,lnmp
目录安装: #!/bin/sh path=/server/scrips [ ! -d "$path" ] && mkdir $path -p #menu cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit] pls input the num you wang: END read num expr $num + 1 &>/dev/null if [ $? -ne 0 ];then echo "the num you input must be {1|2|3}" exit 1 fi [ $num -eq 1 ]&&{ echo "start install lamp." sleep 2; [ -x "$path/lamp.sh" ]||{ echo " does not exist or can not be exec." exit 1 } $path/lamp.sh #source $path/lamp.sh exit $? } if [ $num -eq 2 ];then echo "start install lnmp." sleep 2; 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 3 ];then echo bye exit 3 fi [[ ! $num =~[1-3] ]] &&{ echo "the num you input must be {1|2|3}" echo "Input ERROR" exit 4 }