如何在Bash中工作?
问题描述:
在bash我怎么能做出这样的建设工作:如何在Bash中工作?
if (cp /folder/path /to/path) && (cp /anotherfolder/path /to/anotherpath)
then
echo "Succeeded"
else
echo "Failed"
fi
的,如果要测试$?返回每个命令的代码并将它们与& &联系起来。
我该如何在Bash中做到这一点?
答
if cp /folder/path /to/path /tmp && cp /anotherfolder/path /to/anotherpath ;then
echo "ok"
else
echo "not"
fi
+0
似乎工作,谢谢。 – Dragos 2010-03-24 12:01:35
+2
@Dragos,有礼貌的下一步就是正式接受其中一个答案。 – 2010-03-24 15:25:06
答
cp /folder/path /to/path && cp /anotherfolder/path /to/anotherpath if [ $? -eq 0 ] ; then echo "Succeeded" else echo "Failed" fi
答
另一种方式:
cp /folder/path /to/path && cp /anotherfolder/path /to/anotherpath && {
echo "suceeded"
} || {
echo "failed"
}
我测试了它:
[email protected]:~$ cp test.tex a && cp test.aux b && { echo "haha"; } || { echo "hoho"; }
haha
[email protected]:~$ cp test.ztex a && cp test.aux b && { echo "haha"; } || { echo "hoho"; }
cp: cannot stat `test.ztex': No such file or directory
hoho
[email protected]:~$ cp test.tex a && cp test.zaux b && { echo "haha"; } || { echo "hoho"; }
cp: cannot stat `test.zaux': No such file or directory
hoho
@Dragos,失去了括号;他们所做的只是产生一个额外的子shell(几乎肯定不是你想要的)。 – vladr 2010-03-26 04:03:51