Linux 内联 if shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4986109/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:20:25  来源:igfitidea点击:

Inline if shell script

linuxshellscriptingsh

提问by London

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

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

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 "myApplicationI get 1 output. Is it possible to create result from single line in a script ? thank you

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

采纳答案by Shawn Chin

It doesn't work because you missed out fito end your ifstatement.

它不起作用,因为你错过了fi结束你的if陈述。

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.

另外,请注意@DigitalRoss' answer 中ps -ef | grep ...提到的匹配自身的问题

update

更新

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

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

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

回答by DigitalRoss

Yes, with syntax issues fixed

是的,已修复语法问题

That almostworked. 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 psand grep, the grepwill 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.

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

回答by William Pursell

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:

如果您不想将出现次数限制为恰好 1(例如,如果您只是想检查输出行 myApplication 并且您希望它永远不会出现超过一次),那么只需执行以下操作:

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.

使用短路 && 和 || 运算符通常比嵌入 if/then 结构更清晰,尤其是在单行中。

回答by Tagar

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.

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

Hopefully this will be helpful for somebody else too.

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

回答by minhas23

I am using Mac OS and following worked very well

我正在使用 Mac OS 并且以下效果很好

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

true

真的

Space is needed after [ and before ]

[ ] 之后和之前需要空格