我有一个我目前正在运行的脚本,它对除一个实例之外的所有实例都很有效:
#!/bin/sh
pdfopt test.pdf test.opt.pdf &>/dev/null
pdf2swf test.opt.pdf test.swf
[ "$?" -ne 0 ] && exit 2上面的代码后面还有更多要执行的代码行...
如果"pdf2swf test.opt.pdf test.swf“失败,我该如何更改此脚本以运行"pdf2swf test.pdf test.swf”?如果第二次尝试失败,那么我将"exit 2“。
谢谢
发布于 2010-04-16 22:19:48
尝试:
/path/to/pdfopt test.pdf test.opt.pdf >/dev/null && {
pdf2swf test.opt.pdf test.swf
... maybe do more stuff here, in the future ...
exit_here_nicely
}
code_that_is_reached_if_pdfopt_failed在您的示例中:
pdfopt test.pdf test.opt.pdf &>/dev/null... pdfopt在后台运行,您不知道它可能需要多长时间才能完成。让它阻塞,这样只有当它工作时,才能访问括号中的代码。
一个函数包装,可以很容易地在后台启动,但每个进程都会阻塞,直到第一个命令按预期退出。
发布于 2010-04-16 22:11:27
短路"OR“应该做你想做的事情:
pdf2swf test.opt.pdf test.swf || pdf2swf test.pdf test.swf发布于 2010-04-16 22:07:46
也许你想要一个Makefile而不是一个shell脚本。如果其中一个命令失败,makefile将自动中止。或者,您可以在每个命令后添加[ "$?" -ne 0 ] && exit 2
https://stackoverflow.com/questions/2653483
复制相似问题