如何使反引号在 HERE 文档中起作用?

发布于 2024-12-13 09:17:16 字数 1069 浏览 2 评论 0原文

我有一个 script2:

# This is script2 that is called by script1.    
CURRENT_TOMCAT_PROCESS=`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print $2}'`
echo "---> $CURRENT_TOMCAT_PROCESS"

并且我在 script1: 中调用 script2:

ssh $user@$server 'bash -s' < script2

它工作正常。但我在使反引号在 HERE 文档中工作时遇到了麻烦:(

ssh $user@$server 'bash -s' <<EOF
     CURRENT_TOMCAT_PROCESS=`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'`
     echo "---> $CURRENT_TOMCAT_PROCESS"
EOF

如果我不将其分配给变量而只是将其打印出来,则效果很好,但是当我尝试使用反引号将其分配给 CURRENT_TOMCAT_PROCESS 变量时,它不会不起作用。)

我怎样才能让它起作用?

谢谢,

================================================== =================================

我可以让它按以下方式工作。其中涉及很多转义:

ssh $user@$server 'bash -s' <<EOF
        CURRENT_TOMCAT_PROCESS="\`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'\`"
        echo "---> \$CURRENT_TOMCAT_PROCESS"
EFO

I have a script2:

# This is script2 that is called by script1.    
CURRENT_TOMCAT_PROCESS=`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print $2}'`
echo "---> $CURRENT_TOMCAT_PROCESS"

and I call script2 in script1:

ssh $user@$server 'bash -s' < script2

It works fine. But I'm having trouble make the backtick work in a HERE document:

ssh $user@$server 'bash -s' <<EOF
     CURRENT_TOMCAT_PROCESS=`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'`
     echo "---> $CURRENT_TOMCAT_PROCESS"
EOF

(If I don't assign it to a variable and just print it out it works fine, but when I try to assign it to CURRENT_TOMCAT_PROCESS variable using backticks, it doesn't work.)

How can I make this work?

Thanks,

===============================================================================

I could make it work the following way. There are lots of escaping involved:

ssh $user@$server 'bash -s' <<EOF
        CURRENT_TOMCAT_PROCESS="\`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'\`"
        echo "---> \$CURRENT_TOMCAT_PROCESS"
EFO

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

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

发布评论

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

评论(1

仙气飘飘 2024-12-20 09:17:16

我认为转义是合理的,因为你想将“$”传输到远程站点。你最后的结果似乎有一个拼写错误。我尝试再次在这里输入

TOMCATE_USER=foo
ssh $user@$server 'bash -s' <<EOF
CURRENT_TOMCAT_PROCESS="\`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'\`"
echo "---> \$CURRENT_TOMCAT_PROCESS"
EOF

I think it is reasonable to escape, because you want to transfer the '$' to remote site. You seems make a typo on your last result. I tried to type here again

TOMCATE_USER=foo
ssh $user@$server 'bash -s' <<EOF
CURRENT_TOMCAT_PROCESS="\`ps -ef | grep java | grep $TOMCAT_USER | grep -v grep | awk '{print \$2}'\`"
echo "---> \$CURRENT_TOMCAT_PROCESS"
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文