开发者

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/another开发者_开发知识库path)
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 ?


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜