我想测试两个变量是否存在,在我使用“read”创建它之后.如果用户只输入我想要的两个变量之一,则会显示错误.
有我的代码:
while true;
do
echo "Saisissez deux variables x et y sous la forme [x y]"
read x y
if [ !-e $x ] || [ !-e $y ] <<<<<< problem ligne
then
echo "Vous devez renseigner deux nombres x et y"
elif [ $x = "." ]
then
exit 0
else
calcul $x $y
fi
done
[: !-e: unary operator expected
谢谢你的帮助 :)
解决方法:
将其更改为:
if [ -z "$x" ] || [ -z "$y" ]
说明
> [实际上是一个内置的shell(尝试[或帮助[在你的提示];它是test的同义词.
> -z是[.的参数.它表示“测试下一个字符串的长度是否为0;如果是,则返回true;否则返回false.
>始终用双引号包装您正在测试的变量!
这是一个有用的选项列表[因为我认为你会感兴趣:
-b file = True if the file exists and is block special file.
-c file = True if the file exists and is character special file.
-d file = True if the file exists and is a directory.
-e file = True if the file exists.
-f file = True if the file exists and is a regular file
-g file = True if the file exists and the set-group-id bit is set.
-k file = True if the files "sticky" bit is set.
-L file = True if the file exists and is a symbolic link.
-p file = True if the file exists and is a named pipe.
-r file = True if the file exists and is readable.
-s file = True if the file exists and its size is greater than zero.
-s file = True if the file exists and is a socket.
-t fd = True if the file descriptor is opened on a terminal.
-u file = True if the file exists and its set-user-id bit is set.
-w file = True if the file exists and is writable.
-x file = True if the file exists and is executable.
-O file = True if the file exists and is owned by the effective user id.
-G file = True if the file exists and is owned by the effective group id.
file1 –nt file2 = True if file1 is newer, by modification date, than file2.
file1 ot file2 = True if file1 is older than file2.
file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
-z string = True if the length of the string is 0.
-n string = True if the length of the string is non-zero.
string1 = string2 = True if the strings are equal.
string1 != string2 = True if the strings are not equal.
!expr = True if the expr evaluates to false.
expr1 –a expr2 = True if both expr1 and expr2 are true.
expr1 –o expr2 = True is either expr1 or expr2 is true.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。