shell “integer expression expected”**1


輸入一個成績值,大於90,得A,大於80得B,其他得C!

腳本如下:

  • #!/bin/bash read score

    if ["score" -lt 0 -o "score" -gt 100 ]  then echo "iput:"

    elif [ "score" -ge 90 ]  then echo "A"

    elif ["score" -ge 80 ]  then echo "B"

    else  echo "C" fi

提示以下錯誤:

./score1.sh: line 4: [score: command not found
./score1.sh: line 7: [: score: integer expression expected
./score1.sh: line 10: [score: command not found

改成使用"[[]]",邏輯運算符  "-o" 改成 "||" 即可!

  • #!/bin/bash read score

    if [["score" -lt 0 || "score" -gt 100 ]]  then echo "iput:"

    elif [[ "score" -ge 90 ]]  then echo "A"

    elif [["score" -ge 80 ]]  then echo "B"

    else  echo "C" fi

編輯后能夠達到想要的結果!

  • 總結

    1 所有字符 與邏輯運算符直接用“空格”分開,不能連到一起。

    2 [[]] 運算符只是[]運算符的擴充。里面支持邏輯運算符:|| &&

    3 -gt -ge -lt -le -nt -eq比較的是數字

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM