tags:

views:

95

answers:

3

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 ?

+12  A: 
if cp /folder/path /to/path /tmp && cp /anotherfolder/path /to/anotherpath ;then
  echo "ok"
else
  echo "not"
fi
ghostdog74
Seems to work, thank you.
Dragos
@Dragos, the courteous next step is to formally accept one of these answers.
glenn jackman
+8  A: 
Michael Aaron Safyan
Thank you,$? is the last cp return code not the entire operation ? So is it correct ?
Dragos
Chris Johnsen
@Chris, technically it is called a **pipeline** IIRC, not a "complex" command. :)
vladr
Chris Johnsen
Sorry, yes, "list" is what I was looking for. I'll take the POSIX nomenclature over `dash` any time. :)
vladr
+2  A: 

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
David V.