Shell - 失败时运行附加命令
我当前正在运行这个脚本,该脚本适用于所有实例,但只有一个:
#!/bin/sh
pdfopt test.pdf test.opt.pdf &>/dev/null
pdf2swf test.opt.pdf test.swf
[ "$?" -ne 0 ] && exit 2
要执行的更多行遵循上述代码...
我将如何更改此脚本以运行“pdf2swf test.pdf test.swf”
”如果“pdf2swf test.opt.pdf test.swf
”失败?如果第二次尝试失败,那么我会“exit 2
”。
谢谢
I have this script that I am currently running that works great for all instances but one:
#!/bin/sh
pdfopt test.pdf test.opt.pdf &>/dev/null
pdf2swf test.opt.pdf test.swf
[ "$?" -ne 0 ] && exit 2
More lines to execute follow the above code ...
How would I go about changing this script to run "pdf2swf test.pdf test.swf
" if "pdf2swf test.opt.pdf test.swf
" fails? If the second attempt fails, then I would "exit 2
".
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
短路“OR”应该做你想要的:
Short circuiting "OR" should do what you want:
尝试:
在您的示例中:
...
pdfopt
在后台运行,您不知道可能需要多长时间才能完成。让它阻塞,这样只有在它有效的情况下才能到达括号中的代码。可以在后台轻松启动的函数包装,但每个进程都会阻塞,直到第一个命令按预期退出。
Try:
In your example:
...
pdfopt
runs in the background, you have no idea how long it may take to complete. Let it block, so the code in the parens is reached only if it worked.A function wrapping that could easily launch in the background, but each process blocks until the first command exits as expected.
也许您需要 Makefile 而不是 shell 脚本。当其中一个命令失败时,makefile 会自动中止。或者,您可以添加
[ "$?" -ne 0] &&每个命令后退出 2
Maybe you want a Makefile instead of a shell script. A makefile aborts automatically it one of the commands fail. Alternatively, you can add
[ "$?" -ne 0 ] && exit 2
after every command