Niresh
Niresh

Reputation: 669

Execute a Command if Two Files Exist

Im Trying to Execute a Command if Two Files Exist on a Location

If Only Either One of Two File Exist Script must Execute another different command here is the script i tried

if [ -e /Users/NJ/Library && /Users/NJ/Desktop ];then

echo both

else

echo single

fi

Upvotes: 1

Views: 3023

Answers (4)

Niresh
Niresh

Reputation: 669

if [ -e /Users/NJ/Library ] && [ -e /Users/NJ/Desktop ] ;then

echo both

else

echo single

fi

This Pattern Works Good

Upvotes: 0

Pilou
Pilou

Reputation: 1478

You can use a double bracket insted of a simple :

if [[ -e /Users/NJ/Library && -e /Users/NJ/Desktop ]] ;then

echo both

else

echo single

fi

Upvotes: 0

anishsane
anishsane

Reputation: 20980

if [ -e "/Users/NJ/Library" -a -e "/Users/NJ/Desktop" ];then

....

the [ condition ] evaluator uses -a for AND & -o for OR. Here is a manual.

OR use below syntax.

if [ -e "/Users/NJ/Library" ] && [ -e "/Users/NJ/Desktop" ];then

....

Upvotes: 1

goji
goji

Reputation: 7092

You need to separate the tests into their own square brackets:

if [ -e file ] && [ -e file ]; then
  echo "wow"
fi

Upvotes: 4

Related Questions