Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in bash - unary operator expected with exist -e

Tags:

linux

bash

I want to test if two variables exist, after i used "read" to create it. If the user enter just one of two variables i want, it display an error.

There is my code :

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

and there is the error when i just enter an argument :

[: !-e: unary operator expected

Thanks for your help :)

like image 643
toshiro92 Avatar asked Dec 27 '22 14:12

toshiro92


2 Answers

Change it to:

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

Explanation

  • [ is actually a shell built-in (try which [ or help [ on your prompt); it's is a synonym for test.
  • -z is an argument to [. it means "test if the length of the next string is 0; return true if so; return false otherwise.
  • Always wrap the variables you are testing with double quotes!

Here's a list of useful options to [ since I think you'd be interested:

-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.
like image 115
sampson-chen Avatar answered Jan 05 '23 13:01

sampson-chen


The operator -e is not the right operator to use in this situation. The operator -z is the right operator. This checks to see if a string is empty; in your case x and y.

So change this:

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

to this:

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

The operator -e is used to check if a file exists.

like image 24
Florin Stingaciu Avatar answered Jan 05 '23 13:01

Florin Stingaciu