2017-06-21 168 views
0

試圖編寫一個只接受一個命令行參數的腳本,並且該腳本必須是正數。我有一個論點,但無法弄清楚正數。 我有以下幾點:Linux腳本:命令行參數

#!/bin/ksh 

    NUMBER=$1 
    # Accepts only one argument or does not execute. 
    if [ "$#" != 1 ] 
    then 
    echo "error: program must be executed with 1 argument." 
    exit 
    else 
     if [ "$#" < 1 ] # **NEED HELP HERE**. Check for negative number. 
    then 
     echo "error: argument must be a positive number." 
    fi 
    fi 
    do 
     echo -n $NUMBER 
     if [ $NUMBER -gt 1 ] 
     then 
      echo -n ", " 
     fi 
     NUMBER=$(($NUMBER - 1)) 
    done 
    echo 

...需要幫助確定,如果命令行參數爲負數。

感謝

+0

你認爲什麼是有效的「正數」......只有整數?浮動/真實(例如,3.1415)?指數表示法(例如,1e + 5)?領先的'+'(例如+5)? – markp

+0

是的,只有整數-1,2,3等 – T2true

回答

0

下面的工作:

test "$NUMBER" -ge 0 && printf "NOT NEGATIVE" || printf "NEGATIVE" 
+0

ksh:'NUMBER = 1a;測試「$ NUMBER」-ge 0 && printf「NOT NEGATIVE」|| printf「NEGATIVE」'=>'-ksh:test:1a:算術語法錯誤','NEGATIVE' – markp

+0

這對我沒有用。即使輸入一個正數,我也得到了「論證必須是一個正數」。我在Linux腳本方面很新穎;我還在學習。我不明白這條線。 – T2true

0

驗證它是通過檢查使用數量是否它只是個數字:

#!/bin/ksh 

NUMBER=$1 
# Accepts only one argument or does not execute. 
if [ "$#" != 1 ] 
then 
    echo "error: program must be executed with 1 argument." 
    exit 
else 
    case $NUMBER in 
    ''|*[!0-9]*) 
     echo "error: argument must be a positive number." 
     exit 
     ;; 
    esac 
fi 

while ! [ $NUMBER -lt 1 ] 
do 
    echo -n $NUMBER 
    if [ $NUMBER -gt 1 ] 
    then 
     echo -n ", " 
    fi 
    NUMBER=$(($NUMBER - 1)) 
done 
echo 
+0

這是優秀的失敗者。謝謝。 – T2true

0

假設我們只談論正面(基數爲10)整數...並且不包括逗號,指數表示法和前導加號(+)...

# test for ${NUMBER} containing anything other than digits 0-9 
if [[ "${NUMBER}" = @(*[!0-9]*) ]] 
then 
    echo "error: argument is not a positive number." 
else 
    echo "success: argument is a positive number" 
fi 

顯然它會變得更有趣,因爲你剝離了什麼被定義爲*有效* positive number的假設。


擴大對sqrt163提出的解決方案......一個簡單的功能,應該使我們能夠處理指數符號,浮點/真實數字與領先的加號(+),同時允許調用進程濾除關於語法錯誤的輸入共同的只是問題的消息......

$cat test.sh 
#!/bin/ksh 
NUMBER=$1 
isnumber() 
{ 
    # following should generate a non-zero for negative and non-numerics, 
    # while allowing the parent process to filter out syntax errors 

    [[ "${1}" -gt 0 ]] 
} 

# direct isnumber() output to /dev/null since we're only interested 
# in the return code, and we want to mask any syntax issues if 
# isnumber() is called with a non-numeric argument; unfortunately, 
# for a valid numeric that contains a comma (eg, NUMBER = 1,000) 
# our isnumber() call will fail 

if isnumber ${NUMBER} >/dev/null 2>&1 
then 
    echo "${NUMBER} is a positive number" 
else 
    echo "${NUMBER} is not a positive number" 
fi 

而且一些測試...

$ for s in 1 1.33 1e5 1e-4 +1 +1.65 +1e4 1,000 -1 -1.234 -1e5 -1,000 a b a1b ab1 1ab "a b c" 
do 
test.sh "$s" 
done 
1 is a positive number 
1.33 is a positive number 
1e5 is a positive number 
1e-4 is a positive number 
+1 is a positive number 
+1.65 is a positive number 
+1e4 is a positive number 
1,000 is not a positive number 
-1 is not a positive number 
-1.234 is not a positive number 
-1e5 is not a positive number 
-1,000 is not a positive number 
a is not a positive number 
b is not a positive number 
a1b is not a positive number 
ab1 is not a positive number 
1ab is not a positive number 
a b c is not a positive number 

這讓我們(我相信)取得一個或多個逗號,這可以通過使用任何適合你想象的方法去除逗號來處理。