iT邦幫忙

1

關於SHELL 程式碼的問題

如上最近在練習SHELL可是想要的功能一直無法出來
程式碼如下
#!/bin/bash
read -p "you input address : " x
if [ ${x} -gt 3 ]
then
echo "this math onle therr"
exit 3
fi
*if [ ${x} == [:alpha:] ]
then
echo "this is only math"
exit 4
fi *
read -p "you postic : " c
if [ ! -e ${c} ]
then
echo ${c}"is no exist"
exit 5
fi
echo ${c} 192.168.1.${x}/24"(rw,sec=krb5p)" > /test/nfs
我想要的功能是
當X輸入不是數字的時候會跳出提醒並且結束
請問該修改哪裡呢
~

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
dragonH
iT邦超人 5 級 ‧ 2019-08-21 14:44:54
最佳解答
read -p "you input address : " x
if ! [[ "${x}" =~ ^[0-9]+$ ]]
  then
    echo "this is only math"
    exit 4
elif [ ${x} -gt 3 ]
  then
    echo "this math onle therr"
    exit 3
fi
read -p "you postic : " c
if ! [ -e "${c}" ]
  then
    echo ${c} "is no exist"
    exit 5
fi
echo ${c} 192.168.1.${x}/24"(rw,sec=krb5p)" > /test/nfs

第一次寫

參考1

參考2

我要發表回答

立即登入回答