[英]Code does not work with sh -c, but works on sh directly
$ sh
sh-3.2$ if
> ps -ef | grep apple ;
> then
> echo APPLE
> fi ;
lazer 7584 7571 0 04:36 pts/4 00:00:00 grep apple
APPLE
sh-3.2$ exit
exit
$ which sh
/bin/sh
$ /bin/sh -c if ps -ef | grep apple ; then echo APPLE fi ;
bash: syntax error near unexpected token `then'
$
As above, my simple if statement works as expected when executed line by line but gives me the following error when executed using sh -c
: 如上所述,我的简单if语句在逐行执行时按预期工作,但在使用
sh -c
执行时给出了以下错误:
bash: syntax error near unexpected token `then'
bash:意外令牌附近的语法错误`then'
What am I missing here? 我在这里错过了什么?
Your interactive shell will be escaping the invocation via sh -c
. 您的交互式shell将通过
sh -c
转义调用。 In particular it's taking everyting after the semi-colon as a new statement. 特别是它将分号后的每一个作为一个新的陈述。
Quote everything that you're feeding to /bin/sh
eg 引用你要提供给
/bin/sh
所有东西,例如
$ /bin/sh -c "if ps -ef | grep apple ; then echo APPLE fi ;"
I think you may also need to delimit further using semi-colons given that you're condensing everything onto one line, and would perhaps suggest you could use a heredoc . 我认为您可能还需要使用分号进一步划分,因为您将所有内容压缩到一条线上,并且可能建议您使用heredoc 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.