4

「読み取り」を使用して作成した後、2つの変数が存在するかどうかをテストしたいと思います。ユーザーが必要な2つの変数のいずれかを入力すると、エラーが表示されます。

私のコードがあります:

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かどうかをテストし、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には、 /よりも使いやすくする多くの機能があります。[たとえば、式に直接入力する機能や、自動的に引用するためにパラメーター展開を引用する必要がないという事実などがあります。||&&[[

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 に答える