linux shell 脚本中的一行 if/else 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18179915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One line if/else condition in linux shell scripting
提问by Joshua Strot
I would like to have the equivelant of the following in a one line if/else condition.
我想在一行 if/else 条件中包含以下内容。
$maxline=`cat journald.conf | grep "#SystemMaxUse="`
if [ $maxline == "#SystemMaxUse=" ]
then
sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2
mv journald.conf2 journald.conf;
else
echo "This file has been edited. You'll need to do it manually."
fi
I'm attempting to put this into a one line command. So far I've gotten it all but the else portion of the command. Here's what I have so far...
我正在尝试将其放入单行命令中。到目前为止,除了命令的 else 部分之外,我已经掌握了所有内容。这是我到目前为止...
maxline=`cat journald.conf | grep "#SystemMaxUse="` && if [ $maxline == "#SystemMaxUse=" ]; then sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2 && mv journald.conf2 journald.conf; fi
So how can I include the else portion of the above code into my command? Thank you for your help in advance.
那么如何将上述代码的 else 部分包含到我的命令中呢?提前谢谢你的帮助。
采纳答案by Bryan
It looks as if you were on the right track. You just need to add the else statement after the ";" following the "then" statement. Also I would split the first line from the second line with a semicolon instead of joining it with "&&".
看起来你走在正确的轨道上。您只需要在“;”之后添加 else 语句 遵循“then”语句。此外,我会用分号将第一行与第二行分开,而不是用“&&”连接它。
maxline='cat journald.conf | grep "#SystemMaxUse="'; if [ $maxline == "#SystemMaxUse=" ]; then sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2 && mv journald.conf2 journald.conf; else echo "This file has been edited. You'll need to do it manually."; fi
Also in your original script, when declaring maxline you used back-ticks "`" instead of single quotes "'" which might cause problems.
同样在您的原始脚本中,在声明 maxline 时,您使用了反引号“`”而不是单引号“'”,这可能会导致问题。
回答by erlc
It's not a direct answer to the question but you could just use the OR-operator
这不是问题的直接答案,但您可以使用 OR 运算符
( grep "#SystemMaxUse=" journald.conf > /dev/null && sed -i 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf ) || echo "This file has been edited. You'll need to do it manually."
回答by Beejor
To summarize the other answers, for general use:
总结其他答案,供一般用途:
Multi-line if...thenstatement
多行if...then语句
if [ foo ]; then
a; b
elif [ bar ]; then
c; d
else
e; f
fi
Single-line version
单行版
if [ foo ]; then a && b; elif [ bar ]; c && d; else e && f; fi
if [ foo ]; then a && b; elif [ bar ]; c && d; else e && f; fi
Using the OR operator
使用 OR 运算符
( foo && a && b ) || ( bar && c && d ) || e && f;
( foo && a && b ) || ( bar && c && d ) || e && f;
Notes
笔记
Remember that the AND and OR operators evaluate whether or not the result code of the previous operation was equal to true/success (0
). So if a custom function returns something else (or nothing at all), you may run into problems with the AND/OR shorthand. In such cases, you may want to replace something like ( a && b )
with ( [ a == 'EXPECTEDRESULT' ] && b )
, etc.
请记住,AND 和 OR 运算符评估前一个操作的结果代码是否等于 true/success ( 0
)。因此,如果自定义函数返回其他内容(或根本不返回任何内容),您可能会遇到 AND/OR 速记问题。在这种情况下,您可能想要替换诸如( a && b )
with 之类的东西( [ a == 'EXPECTEDRESULT' ] && b )
。
Also note that (
and [
are technically commands, so whitespace is required around them.
另请注意,(
和[
是技术上的命令,因此它们周围需要空格。
Instead of a group of &&
statements like then a && b; else
, you could also run statements in a subshell like then $( a; b ); else
, though this is less efficient. The same is true for doing something like result1=$( foo; a; b ); result2=$( bar; c; d ); [ "$result1" -o "$result2" ]
instead of ( foo && a && b ) || ( bar && c && d )
. Though at that point you'd be getting more into less-compact, multi-line stuff anyway.
除了&&
像 那样的一组语句then a && b; else
,您还可以在像 那样的子shell 中运行语句then $( a; b ); else
,尽管这样效率较低。执行类似result1=$( foo; a; b ); result2=$( bar; c; d ); [ "$result1" -o "$result2" ]
而不是( foo && a && b ) || ( bar && c && d )
. 虽然到那时你会更多地进入不那么紧凑的多行内容。