2015-11-03 17 views
-2

我剛學習shell腳本!我嘗試下面的代碼,但我有錯誤unix所有if語句在一個地方

線15:附近意外的標記`elif的語法錯誤

#!/bin/bash 

read -p "Enter the number : " n 

if [ $n -eq 1 ] 
then 
    echo "$n is equal to 1 true" 

    elif [ $n -lt 4 ] 
    then 
     echo "$n is less than value of 4 ture" 
    else 
     echo "$n is not less then value of 4 false" 

    elif [ $n -gt 10 ] 
    then 
     echo "$n is greater than the value of 10 true" 
    else 
     echo "$n is not greater than the value of 10 false" 

    if [ $n -ge 0 ] 
    then 
     echo "$n is greater than or equal to 0" 
    else 
     echo "$n is not greater than or equal to 0" 
    fi 
else 
    "Bye" 
fi 

誰能幫助?

+3

'if' /'else'或'if' /'elif' /' else'或'if /' elif' /'elif' ... /'else '但不是'if'/'elif' /'else' /'elif' /'else'。 –

+1

在http://www.shellcheck.net/中粘貼代碼顯示您缺少一些'fi's。 – fedorqui

+0

Hay fedorqui感謝您的shellcheck鏈接 – viswa

回答

1

說出你的意圖有點難。正確的答案取決於你想在這裏做什麼。我的猜測是你想要這個:

if [ $n -eq 1 ] 
then 
    echo "$n is equal to 1 true" 

elif [ $n -lt 4 ] 
then 
    echo "$n is less than value of 4 true" 

elif [ $n -gt 10 ] 
then 
    echo "$n is greater than the value of 10 true" 

elif [ $n -ge 0 ] 
then 
    # This will be true if $n is >= 4 and <= 10 
    echo "$n is greater than or equal to 0" 
else 
    # Negative 
    "Bye" 
fi