4

在我使用“读取”创建它之后,我想测试两个变量是否存在。如果用户只输入我想要的两个变量之一,则会显示错误。

有我的代码:

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

谢谢你的帮助 :)

4

3 回答 3

9

将其更改为:

if [ -z "$x" ] || [ -z "$y" ]

解释

  • [实际上是一个内置的外壳(尝试which [help [在您的提示下);它是 的同义词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.
于 2012-11-22T21:14:31.987 回答
1

运算符-e不是在这种情况下使用的正确运算符。操作员-z是正确的操作员。这将检查字符串是否为空;在你的情况下xy

所以改变这个:

if [ !-e $x ] || [ !-e $y ]

对此:

if [ ! -z $x ] || [ ! -z $y ]

运算符 -e 用于检查文件是否存在。

于 2012-11-22T21:16:20.540 回答
0

在 bash 中,您可以使用:

if [[ -z $x || -z $y ]]; then

[[test有许多比/更容易使用的特性[,包括将||and&&直接放入表达式的能力,以及您不需要引用参数扩展这一事实,因为[[它会自动执行。

help [[

将为您提供更多信息,尽管它遗漏了这个有用的段落,您可以在以下位置找到man bash

  Word splitting and pathname expansion are not performed on the
  words between the [[ and ]]; tilde expansion, parameter and
  variable expansion, arithmetic expansion, command substitution,
  process substitution, and quote removal are performed. Conditional
  operators such as -f must be unquoted to be recognized as primaries.
于 2012-11-22T23:47:08.883 回答