[::整数表达式预期错误

时间:2014-09-30 00:53:07

标签: linux bash shell scripting

我遇到了这个脚本的问题,并且不确切知道发生了什么。

#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”]时收到错误;

4 个答案:

答案 0 :(得分: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 $inputinput没有意义。要设置其值的其他变量。

答案 1 :(得分: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

您可能希望使用关联数组来构建调度表。

答案 2 :(得分:0)

问题出在

while [ "$input" -eq "0" ]

并且您还没有使用while循环结束语句done

在比较两个字符串时,您应该使用

while [[ $input == 0 ]]; do <statements>; done

while [ $input = 0 ]; do <statements>; done

所以你的代码将是:

#Main Menu
clear
echo -n "Select an operation for me to do: " 
read input

while [ $input = 0 ]
do
        clear
done

#$input == 0
echo "Hello, What would you like me to do?"
echo ""
echo "1 = ALL THE 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"

exit 0

答案 3 :(得分:-3)

-eq仅适用于整数。

$input0不应该在引号中。

要将$input转换为整数,请改用$((input))

你似乎在某个地方错过了done