如何使"如果没有真实情况和QUOT ;? [英] How to make "if not true condition"?

查看:133
本文介绍了如何使"如果没有真实情况和QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有回声命令执行时猫/ etc / passwd文件| grep的sysA的形式是不正确的。

我在做什么错了?

 如果! [$(CAT / etc / passwd文件| grep的sysA的形式)];然后
        回声错误 - 用户sysA中无法抬头
        2号出口
科幻


解决方案

尝试

 如果! grep的-q sysA中/ etc / passwd文件;然后

的grep 收益真正如果找到搜索目标,而,如果它没有。

所以不是 == 真正

在弹

如果评价都设计得非常灵活,而且很多时候并不需要的命令链(为你写)。

此外,在您的code作为期待,你的CMD替代的 $(...)形式的使用是值得赞扬的,但想想什么是走出来的过程。尝试回声$(CAT / etc / passwd文件| grep的sysA的形式)来看看我的意思。您可以通过使用 -c (计数)选项到grep采取进一步再去做如果! [$(grep的-csysA的形式/ etc / passwd文件)-eq 0];然后这工作,相反却是老同学。

但你可以使用最新的外壳功能(算术评估)像

 如果! (($(grep的-csysA的形式/ etc / passwd文件)== 0));然后...`

这也给了你使用的C浪基于比较运营商的利益, ==<,>,> =,< =,%和其他人也许几个。

在这种情况下,通过每一个Orwellophile评论,算术评估可以进一步缩减下来,像

 如果! (($(grep的-csysA的形式/ etc / passwd文件)));然后 ....

  IF(($(grep的-csysA的形式/ etc / passwd文件))!);然后 ....

最后,还有一个的的猫叫(UUOC)的没用使用。 :-)有些人会跳起来并大声呼喊gothca!我只想说,的grep 能就其CMD线的文件名,那么为什么调用额外的流程和管结构,当你不就得了? ;-)

我希望这有助于。

I would like to have the echo command executed when cat /etc/passwd | grep "sysa" is not true.

What am I doing wrong?

if ! [ $(cat /etc/passwd | grep "sysa") ]; then
        echo "ERROR - The user sysa could not be looked up"
        exit 2
fi

解决方案

try

  if ! grep -q sysa /etc/passwd ; then

grep returns true if it finds the search target, and false if it doesn't.

So NOT false == true.

if evaluation in shells are designed to be very flexible, and many times don't require chains of commands (as you have written).

Also, looking at your code as is, your use of the $( ... ) form of cmd-substitution is to be commended, but think about what is coming out of the process. Try echo $(cat /etc/passwd | grep "sysa") to see what I mean. You can take that further by using the -c (count) option to grep and then do if ! [ $(grep -c "sysa" /etc/passwd) -eq 0 ] ; then which work but is rather old school.

BUT, you could use the newest shell features (arithmetic evaluation) like

if ! (( $(grep -c "sysa" /etc/passwd) == 0 )) ; then ...`

which also gives you the benefit of using the c-lang based comparison operators, ==,<,>,>=,<=,% and maybe a few others.

In this case, per a comment by Orwellophile, the arithmetic evaluation can be pared down even further, like

if ! (( $(grep -c "sysa" /etc/passwd) )) ; then ....

OR

if (( ! $(grep -c "sysa" /etc/passwd) )) ; then ....

Finally, there is an award called the Useless Use of Cat (UUOC). :-) Some people will jump up and down and cry gothca! I'll just say that grep can take a file name on its cmd-line, so why invoke extra processes and pipe constructions when you don't have to? ;-)

I hope this helps.

这篇关于如何使&QUOT;如果没有真实情况和QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆