我不明白为什么它这么说“需要整数表达式”

我不明白为什么它这么说“需要整数表达式”

所以这是我的代码

#!bin/bash

PS3='Pick the one you like and we will continue: '

select color in "Blue" "Black" "Orange" "Yellow"
do
echo "You selected $color"
break
done

echo

var=":"

echo "Alrighty, please type in the password as you have specified the    color $color"

read var

until  [ "$var" -ne password ] 
do
echo "You have specified a wrong password, and a wrong color please leave the script the script"
break
done

终端响应如下:

1) Blue
2) Black
3) Orange
4) Yellow
Pick the one you like and we will continue: 1
You selected Blue

Alrighty, please type in the password as you have specified the color Blue
23
/root/Desktop/Bash/Test_1.sh: line 16: [: password: integer expression  expected
You have specified a wrong password, and a wrong color please leave the script

答案1

您正在做:

[ "$var" -ne password ]

-ne(not equal) 是 ie 的整数运算符,[它期望两边都是整数。

但是var=":"一开始,尽管您正在read-ing 用户输入并将其放入 中var,但输入可能不是整数,从而导致错误消息。

无论如何,如果它是一个整数,检查就会失败,因为它password本身就是一个字符串。

如果您想比较字符串是否相等,则运算符是=

[ "$var" = password ]

不等式:

[ "$var" != password ]

相关内容