Shell編程進階 1.7 case選擇

邏輯判斷的格式vim

vim case.sh
#!/bin/bash
read -p "please input a number:" n
m=$[$n%2]
case $m in
        1)
                echo " It is a jishu'"
                ;;
        0)
                echo " It is a oushu."
                ;;
        *)
                echo " Not is a jishu and oushu."
esac

輸入一串字符 賦值$nbash

$m=$n除以2spa

判斷$m code

等於1blog

輸出這是個奇數input

等於0class

輸出這是個偶數sed

等於其餘im

輸出這不是奇數和偶數di

#!/bin/bash
read -p "please input a number:" n
w=`echo $n |sed 's/[0-9]//g'`
if [ -n "$w" ]
then
        echo "Not is a number,please input a number."
else
        echo $n
fi
m=$[$n%2]
case $m in
        1)
                echo " It is a jishu'"
                ;;
        0)
                echo " It is a oushu."
                ;;
        *)
                echo " Not is a jishu and oushu."
esac
相關文章
相關標籤/搜索