Shell - 失败时运行附加命令

发布于 2024-08-29 00:23:38 字数 371 浏览 3 评论 0原文

我当前正在运行这个脚本,该脚本适用于所有实例,但只有一个:

 #!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-09-05 00:23:38

短路“OR”应该做你想要的:

pdf2swf test.opt.pdf test.swf || pdf2swf test.pdf test.swf

Short circuiting "OR" should do what you want:

pdf2swf test.opt.pdf test.swf || pdf2swf test.pdf test.swf
情深缘浅 2024-09-05 00:23:38

尝试:

/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 在后台运行,您不知道可能需要多长时间才能完成。让它阻塞,这样只有在它有效的情况下才能到达括号中的代码。

可以在后台轻松启动的函数包装,但每个进程都会阻塞,直到第一个命令按预期退出。

Try:

/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

In your example:

pdfopt test.pdf test.opt.pdf &>/dev/null

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

我的痛♀有谁懂 2024-09-05 00:23:38

也许您需要 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文