内联 if shell 脚本

发布于 2024-10-17 03:26:25 字数 295 浏览 7 评论 0原文

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

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

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

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

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";
>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心意如水 2024-10-24 03:26:26

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

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

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

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

另外,请注意 ps -ef | 的问题。 grep ... 匹配 @DigitalRoss 的回答。

更新

事实上,您可以使用 pgrep 做得更好:

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

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

Also, do take note the issue of ps -ef | grep ... matching itself as mentioned in @DigitalRoss' answer.

update

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

if [ $(pgrep -c "myApplication") -eq 1 ]; then echo "true"; fi
梦在深巷 2024-10-24 03:26:26

其他响应已经解决了您的语法错误,但我强烈建议您将该行更改为:

test $(ps -ef | grep -c myApplication) -eq 1 && echo true

如果您不尝试将出现次数限制为正好 1(例如,如果您只是尝试检查输出行 myApplication 并且您期望它永远不会出现多次)然后只需执行以下操作:(

ps -ef | grep myApplication > /dev/null && echo true

如果您需要设置变量计数器以供以后处理,那么这些解决方案都不合适。

)和 ||运算符通常比嵌入 if/then 结构更清晰,尤其是在单行语句中。

Other responses have addressed your syntax error, but I would strongly suggest you change the line to:

test $(ps -ef | grep -c myApplication) -eq 1 && echo true

If you are not trying to limit the number of occurrences to exactly 1 (eg, if you are merely trying to check for the output line myApplication and you expect it never to appear more than once) then just do:

ps -ef | grep myApplication > /dev/null && echo true

(If you need the variable counter set for later processing, neither of these solutions will be appropriate.)

Using short circuited && and || operators is often much clearer than embedding if/then constructs, especially in one-liners.

谁许谁一生繁华 2024-10-24 03:26:26

是的,修复了语法问题

几乎可以工作。正确的语法是:

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

但请注意,在涉及 psgrep 的此类表达式中,grep 通常会匹配自身,因为字符 " grep -c Myapplication”出现在 ps 列表中。有几种方法可以解决这个问题,其中之一是 grep 查找类似 [M]yapplication 的内容。

Yes, with syntax issues fixed

That almost worked. The correct syntax is:

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

But note that in an expression of this sort involving ps and grep, the grep will usually match itself because the characters "grep -c Myapplication" show up in the ps listing. There are several ways around that, one of them is to grep for something like [M]yapplication.

森林很绿却致人迷途 2024-10-24 03:26:26

我正在使用 Mac OS,并且以下工作非常好

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

true

之后 [ 和之前 ] 需要空间

I am using Mac OS and following worked very well

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

true

Space is needed after [ and before ]

锦爱 2024-10-24 03:26:26

我正在努力将多行输入合并到命令中并将其结果放入变量(而不是文件)中,并提出这个解决方案:

    FRA_PARAM="'db_recovery_file_dest'"
    FRA=$(
    sqlplus -S "/as sysdba" <<EOF
set echo off head off feed off newpage none pages 1000 lines 1000
select value from v\$parameter where name=$FRA_PARAM;
exit;
EOF
        )

请注意,单引号单词已被替换,因为否则我会收到它对双引号的自动替换... ksh、HP-UX。

希望这对其他人也有帮助。

I was struggling to combine both multiple lines feed into command and getting its results into a variable (not a file) and come up with this solution:

    FRA_PARAM="'db_recovery_file_dest'"
    FRA=$(
    sqlplus -S "/as sysdba" <<EOF
set echo off head off feed off newpage none pages 1000 lines 1000
select value from v\$parameter where name=$FRA_PARAM;
exit;
EOF
        )

Please note that single-quotes word was substituted, because otherwise I was receiving its autosubstitution to double quotes... ksh, HP-UX.

Hopefully this will be helpful for somebody else too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文