[英]Stop bash on error same as “set -e” in script file
I have an external command to produce some bash commands, let's call it gen_commands
, it produces a list of commands that I need to run with bash
, but if any command fails I need to stop and exit. 我有一个外部命令来生成一些bash命令,我们将其
gen_commands
,它会生成一个需要使用bash
运行的命令列表,但是如果任何命令失败,则需要停止并退出。 To stop it on error I'm writting the result of gen_commands
to temporary script file appending set -e
at the beggining: 为了在错误时停止它,我将
gen_commands
的结果gen_commands
临时脚本文件中,在开始时附加set -e
:
echo "#!/bin/bash" > tmp.sh
echo "set -e" >> tmp.sh
gen_commands >> tmp.sh
chmod +x tmp.sh
./tmp.sh
rm tmp.sh
It would look much cleaner if I can run it with one line: 如果我可以一行运行,它将看起来更加干净:
gen_commands | bash
but in that case bash
ignores errors. 但是在那种情况下,
bash
忽略错误。 Is it possible to configure bash to fail on error without writting to sccript file. 是否有可能将bash配置为在失败时失败而无需编写脚本文件。
You can use a command group to combine the output of multiple commands into one stream: 您可以使用命令组将多个命令的输出组合到一个流中:
{ echo "set -e"; gen_commands; } | bash
However, you can simply pass -e
as an option to bash
as well: 但是,您也可以简单地通过
-e
作为bash
的选项:
gen_commands | bash -e
From the man page: 从手册页:
OPTIONS
OPTIONS
All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked.
set内置命令的描述中记录的所有单字符外壳程序选项都可以在调用外壳程序时用作选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.