如何将bash脚本语句组合到一个
我正在寻找&& ||而是“ if”“否则” bash脚本中的块,
如何将下面的两个语句组合为一个。
[[ $? -ne 0 ]] && echo "Add User failed" && exit 1
[[ $? -eq 0 ]] && echo "User added successfully"
请帮忙!
I am looking to use &&, || instead "if" "else" block in bash script
How do I combine below two statements into one.
[[ $? -ne 0 ]] && echo "Add User failed" && exit 1
[[ $? -eq 0 ]] && echo "User added successfully"
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,我建议不要尝试使用
&&
和||
替换如果...然后... else
块。如果
语句具有清晰而直接的语义,但是& amp; amp;
和||
的组合通常以意外且难以理解的方式进行交互。服用
[[$? -ne 0]]&&回声“添加用户失败”& amp;出口1
。问题是退出
仅在[[$?? -ne 0]]
是正确的(即,如果以前的命令失败),以及echo
命令成功(这就是最终& &
表示)。现在echo
是一个非常简单且可靠的命令,几乎永远不会失败。但是,如果事情变得足够混乱,以至于echo
无法运行,我敢肯定,您真的真的真的希望脚本退出而不是尝试继续。它的“正确”版本将是
[[$? -ne 0]]&& {echo“添加用户失败”>& 2;出口1; }
。在这里,{;}
使echo
和exit
组一起执行,;
之间他们意味着退出
是否会执行echo
成功。顺便说一句,我还向>> 2
添加到echo
命令中,以将其消息发送到标准错误(错误和状态消息的正确目标),而不是标准输出。您的第二个命令
[[$? -eq 0]]&& echo“用户成功添加”
,没有这个问题,但是它确实存在$?
将是前一个命令的状态...哪个是[[$? -ne 0]]&& ...
事物,而不是您实际要检查的命令。这是使用$?
的常见问题,因为它被Shell所做的每一件事所取代。如果您想在单个命令的退出状态上运行多个测试,则几乎需要立即将其存储在变量中,然后测试:但是,在您的情况下,您实际上并不需要第二个测试;第一个应该退出脚本,这意味着,如果它到达第二个脚本,那么原始命令必须成功。另外,在大多数情况下(包括此),最好不要使用
$?
,而只是直接在测试中使用命令。因此,这是||
的少数用途之一,我考虑了良好的练习:请注意,这使用
||
而不是&&&
,因为如果命令失败,则应运行错误处理程序,而不是成功。另外,此组合之所以安全的唯一原因是,仅涉及一个||
(可能是因为错误处理程序退出),而不是多个互相缠结的。如果错误条件未退出,则您确实应该使用适当的(如果
语句)获得可预测的结果:或者如果您希望它在一行:
In general, I recommend against trying to use
&&
and||
to replaceif ... then ... else
blocks.if
statements have clear and straightforward semantics, but combinations of&&
and||
often interact in unexpected and hard-to-understand ways.Take
[[ $? -ne 0 ]] && echo "Add User failed" && exit 1
. The problem here is thatexit
only gets executed if[[ $? -ne 0 ]]
is true (i.e. if the previous command failed), and also theecho
command succeeds (that's what that final&&
means). Nowecho
is a pretty simple and reliable command, and will almost never fail; but if things are messed up enough thatecho
can't run, I'm pretty certain you really really really want the script to exit rather than trying to continue.The "correct" version of this would be
[[ $? -ne 0 ]] && { echo "Add User failed" >&2; exit 1; }
. Here, the{ ;}
makes theecho
andexit
a group that gets executed together, and the;
between them means thatexit
gets executed whether or notecho
succeeds. BTW, I also added>&2
to theecho
command to send its message to standard error (the correct destination for errors and status messages) instead of standard output.Your second command,
[[ $? -eq 0 ]] && echo "User added successfully"
, doesn't have that problem, but it does have the problem that$?
is going to be the status of the immediately previous command... which is the[[ $? -ne 0 ]] && ...
thing, not the command you actually want to check. This is a common problem with using$?
, because it gets replaced by every single thing the shell does. If you want to run multiple tests on the exit status of a single command, you pretty much need to immediately store it in a variable, and then test that:In your situation, though, you don't really need the second test; the first one should exit the script, meaning that if it gets to the second one the original command must've succeeded. Also, in most cases (including this one), you're better off not using
$?
at all, and just using the command directly in the test. So this is one of the few uses of||
I consider good practice:Note that this uses
||
instead of&&
, because the error handler should run if the command fails, not if it succeeds. Also, the only reason this combo is safe is because there's only a single||
involved (possible because the error handler exits), not multiple ones to get tangles up with each other. If the error condition didn't exit, you really should use a properif
statement to get predictable results:Or if you want it on a single line:
如果我对您要做的事情做出一些常识的猜测,我会到达:
If I make some common-sense guesses about what you're trying to do, I arrive at:
在Bash程序中,使用“模具”功能打印错误消息并退出程序很常见。参见 bashfaq/101-公共实用功能(warn,die,die)。 (还有在bash中,是否有相当于“ die”错误“错误msg” ,但是所有答案都有问题。)使用
die
函数,您可以简单地做:It is common in Bash programs to use a "die" function to print an error message and exit the program. See BashFAQ/101 - Common utility functions (warn, die). (There's also In bash, is there an equivalent of die "error msg", but all of the answers have problems.) With a
die
function you can simply do: