1. 程式人生 > >【shell 練習1】編寫Shell條件句練習

【shell 練習1】編寫Shell條件句練習

scrip 通過 AS ror one inpu BE pat shel

實例一、比較兩個整數大小

#!/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 }

【shell 練習1】編寫Shell條件句練習