Dragos
Dragos

Reputation: 241

How can I make this if's work in Bash?

In bash how can I make a construction like this to work:

if (cp /folder/path /to/path) && (cp /anotherfolder/path /to/anotherpath)
then
  echo "Succeeded"
else
  echo "Failed"
fi

The if should test for the $? return code of each command and tie them with &&.

How can I make this in Bash ?

Upvotes: 4

Views: 1156

Answers (3)

David V.
David V.

Reputation: 5708

Another way :

cp /folder/path /to/path && cp /anotherfolder/path /to/anotherpath && {
  echo "suceeded"
} || {
  echo "failed"
}

I tested it :

david@pcdavid:~$ cp test.tex a && cp test.aux b && { echo "haha"; } || { echo "hoho"; }
haha
david@pcdavid:~$ cp test.ztex a && cp test.aux b && { echo "haha"; } || { echo "hoho"; }
cp: cannot stat `test.ztex': No such file or directory
hoho
david@pcdavid:~$ cp test.tex a && cp test.zaux b && { echo "haha"; } || { echo "hoho"; }
cp: cannot stat `test.zaux': No such file or directory
hoho

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342373

if cp /folder/path /to/path /tmp && cp /anotherfolder/path /to/anotherpath ;then
  echo "ok"
else
  echo "not"
fi

Upvotes: 13

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

cp /folder/path /to/path && cp /anotherfolder/path /to/anotherpath
if [ $? -eq 0 ] ; then
    echo "Succeeded"
else
    echo "Failed"
fi

Upvotes: 8

Related Questions