什麼是錯的,我得到一個錯誤:./greater[4]: [: argument expected
的Unix編程if語句
#!/bin/ksh
echo "please enter a nuber "
read $num
if [$num -ls 20 ]; then
echo "your nuber is greater than 20 "
else
echo "your number is less then 20 "
fi
什麼是錯的,我得到一個錯誤:./greater[4]: [: argument expected
的Unix編程if語句
#!/bin/ksh
echo "please enter a nuber "
read $num
if [$num -ls 20 ]; then
echo "your nuber is greater than 20 "
else
echo "your number is less then 20 "
fi
read $num
上面將讀取輸入到其中的名稱存儲在$num
一個變量,其中「明顯」是不正確的。將其更改爲read num
,它將按預期工作。
對於上述變化,你還需要上線[
後添加一個空格引用如下:
if [$num -ls 20 ]; then
否則,命令將類似於下面的代碼片段,如果用戶輸入,你的shell會抱怨找不到命令[123
或類似的東西。
if [123 -ls 20 ]; then
太棒了,謝謝 – toky 2012-03-03 22:41:44
您[
後需要一個空間:
if [ $num -lt 20 ]; then
的[
命令實際上是同樣的事情test
命令,作爲命令,它需要在命令名和第一個參數之間有一個空格。
我仍然有同樣的錯誤 – toky 2012-03-03 22:33:12
另外,你的邏輯是倒退:如果'[$ NUM -lt 20]'('-ls'是不正確的)那麼「你的號碼是小於20」不大於 – 2012-03-04 01:55:06