2014-09-30 83 views
0

我遇到了這個腳本的問題,不知道到底發生了什麼。[::整數表達式預計錯誤

#Main Menu 
clear 
while [ "$input" -eq "0" ]; 
do 
clear 
#$input == 0 
echo "Hello, What would you like me to do?" 
echo "" 
echo "1 = ALL TEH THINGZ!" 
echo "2 = Find the enemy postions and villages" 
echo "3 = Set the firewalls to my liking" 
echo "4 = Confuse the enemy" 
echo "5 = Deploy the spies!" 
echo "6 = DISABLED Run the TOP SECRET STUFF! (Note password is required)" 
echo "81 = Burn the bridges! (Cautian: Last Resort)" 
echo "96 = Install/Update Software (locally)" 
echo "97 = Install/Update Software (remotely)" 
echo "98 = Update and add new Kali tools" 
echo "99 = I am done with you for now" 
read -p "Select an operation for me to do: " $input 

我知道我有fi和結束右,但我在while [「$ input」-eq「0」];

+3

此外,替換'讀-p「選擇一個操作爲我做:」$輸入「與'讀-p」選擇一個操作對我來說:「輸入' – John1024 2014-09-30 00:59:53

+0

從http://stackoverflow.com/ help/mcve - 「儘可能少使用代碼」(而不是將90%的代碼作爲與問題無關的「echo」語句)。如果您「發現錯誤」,請在問題本身中提供* exact *錯誤。理想情況下,這可能只有兩行 - 一個設置'input =「something」'和另一個正在運行的'[「$ input」-eq「0」]'(如果'read'的行爲被正確診斷,問題)。 – 2017-03-07 03:44:51

回答

4

如果$input不是合法的數值表達式,您將收到該消息;例如,如果它是空字符串。如果要循環,而它的0,你應該將其設置爲0。

input=0 
while [ "$input" -eq 0 ]; do 
... 
done 

雖然你已經標記此bash開出;如果你真的使用bash,你也可以這樣做:

while ((input == 0)); do 
... 
done 

請注意,shell不是Perl; $不是變量名稱的一部分。將$認爲是「獲得價值」的運營商會更好。該變量是input;您將其指定爲input=;你用read input讀入它; 。你只需要使用$input當你想獲得存儲在這個變量中的值。

所以,舉例來說,它沒有任何意義做$input=read $input,除非存儲在input變量中的值是一個字符串,它是要設置的值一些其他變量的名稱。

-3

-eq僅適用於整數。

$input0不應該在引號中。

要將$input轉換爲整數,請改爲使用$((input))

你似乎在某處丟失了done

+1

引號不是問題;在這種情況下,他們確實會改變錯誤信息,但是如果'input'保持一個數字值,他們根本就不會妨礙。使用'$((input))'會成功地將空字符串變爲0,但這只是掩蓋了問題,它使用了一個未初始化的變量。 – 2014-09-30 01:17:39

1

你應該在這裏使用一點讚賞的select命令。用適當的操作替換:命令。

choices=(
    'ALL TEH THINGZ!' 
    'Find the enemy postions and villages' 
    'Set the firewalls to my liking' 
    'Confuse the enemy' 
    'Deploy the spies!' 
    'DISABLED Run the TOP SECRET STUFF! (Note password is required)' 
    'Burn the bridges! (Cautian: Last Resort)' 
    'Install/Update Software (locally)' 
    'Install/Update Software (remotely)' 
    'Update and add new Kali tools' 
    'I am done with you for now' 
) 
PS3='Select an operation for me to do: ' 
echo 'Hello, What would you like me to do?' 

select choice in "${choices[@]}"; do 
    case $choice in 
     'ALL TEH THINGZ!') 
      : 
      ;; 
     'Find the enemy postions and villages') 
      : 
      ;; 
     'Set the firewalls to my liking') 
      : 
      ;; 
     'Confuse the enemy') 
      : 
      ;; 
     'Deploy the spies!') 
      : 
      ;; 
     'DISABLED Run the TOP SECRET STUFF! (Note password is required)') 
      : 
      ;; 
     'Burn the bridges! (Cautian: Last Resort)') 
      : 
      ;; 
     'Install/Update Software (locally)') 
      : 
      ;; 
     'Install/Update Software (remotely)') 
      : 
      ;; 
     'Update and add new Kali tools') 
      : 
      ;; 
     'I am done with you for now') 
      break 
      ;; 
    esac 
done 

您可能想要使用關聯數組來構造一個調度表。