内联if shell脚本 [英] Inline if shell script

查看:46
本文介绍了内联if shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在命令行中像这样执行shell脚本:

Is it possible to execute shell script in command line like this :

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ] then; echo "true";
>

上面的示例不起作用,我只得到> 字符而不是我要获得的结果,即"true"

Above example is not working I get only > character not the result I'm trying to get, that is "true"

当我执行 ps -ef |grep -c "myApplication 我得到 1 个输出.是否可以从脚本中的单行创建结果?谢谢

When I execute ps -ef | grep -c "myApplication I get 1 output. Is it possible to create result from single line in a script ? thank you

推荐答案

它不起作用,因为您错过了 fi 来结束 if 语句.

It doesn't work because you missed out fi to end your if statement.

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true"; fi

您可以使用以下方法进一步缩短它:

You can shorten it further using:

if [ $(ps -ef | grep -c "myApplication") -eq 1 ]; then echo "true"; fi

还请注意 ps -ef |的问题.grep ... 匹配 @DigitalRoss的答案>.

实际上,您可以使用 pgrep 做一个更好的事情:

In fact, you can do one better by using pgrep:

if [ $(pgrep -c "myApplication") -eq 1 ]; then echo "true"; fi

这篇关于内联if shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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