bash 中的一行条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4076561/
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 condition in bash
提问by SIMEL
I'm asked in my course HW to write a comparison in bash using just one line and without ';'. I am required to check whether the string in the variable 'fname' ends with the letter 'C', and if so to print "Match". There is no else command. how can I do it in one line?
在我的课程 HW 中,我被要求只使用一行而不使用“;”在 bash 中编写一个比较。我需要检查变量“fname”中的字符串是否以字母“C”结尾,如果是,则打印“Match”。没有其他命令。我怎样才能在一行中做到这一点?
回答by Benoit
Do you know of the &&, ||and &command terminators in bash?
您知道bash中的&&,||和&命令终止符吗?
[[ "${fname:(-1)}" == "C" ]] && echo Match
回答by Julian
I'm evil. I like being "clever":
我很邪恶。我喜欢“聪明”:
echo ${fname}|sed -e 's/^.*\(.\)$/\1/' -e 's/[^C]/No /' -e 's/.$/Match/'
echo ${fname}|sed -e 's/^.*\(.\)$/\1/' -e 's/[^C]/No /' -e 's/.$/Match/'
J
J

