如果在shell中的一行中写其他字符,该如何写? [英] How to write if else in one line in shell?

查看:98
本文介绍了如果在shell中的一行中写其他字符,该如何写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样写:

if [$SERVICESTATEID$ -eq 2]; then echo "CRITICAL"; else echo "OK"; fi

要在外壳中进行测试,我做了:

So to do a test in my shell I did:

if [2 -eq 3]; then echo "CRITICAL"; else echo "OK"; fi

结果是

-bash: [2: command not found
OK

所以它不起作用.

推荐答案

空间-最后的领域.这有效:

Space -- the final frontier. This works:

if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi

注意[之后和]之前的空格-[是命令名称!然后我在$SERVICESTATEID的末尾删除了一个额外的$.

Note spaces after [ and before ] -- [ is a command name! And I removed an extra $ at the end of $SERVICESTATEID.

另一种方法是拼出test.然后,您不需要最终的],这就是我更喜欢的:

An alternative is to spell out test. Then you don't need the final ], which is what I prefer:

if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi

这篇关于如果在shell中的一行中写其他字符,该如何写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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