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

问题描述 投票:1回答:4

我有问题,这个脚本,不知道到底发生了什么。

#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

我知道我有网络连接,并最终正确的,但我在,而[“$输入” -eq“0”]得到一个错误;

linux bash shell scripting
4个回答
4
投票

你会得到消息,如果$input是不是合法的数值表达式;举例来说,如果它是一个空字符串。如果要循环,而它的0,你应该将其设置为0开始的。

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

虽然你已经标记此bash;如果你实际上使用bash,你也可以这样做:

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

需要注意的是外壳并不是Perl,该$不是变量名的一部分。这是更好地认为$作为运营商,这意味着“得到的价值。”变量是input;你input=它分配;你读入它read input;等等。当你想获得存储在变量值仅能使用$input

所以,举例来说,它没有任何意义做$input=read $input除非存储在input变量中的值是一个字符串,它是要设置的值,其他一些变量的名称。


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

你可能要考虑使用关联数组构造调度表。


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
投票

-eq适用于只是整数。

$input0不应该加引号。

要转换$input为整数,而是使用$((input))

你似乎在什么地方缺少done

© www.soinside.com 2019 - 2024. All rights reserved.