shell 脚本中 HERE 文档中的引号

发布于 2024-12-14 09:35:45 字数 844 浏览 1 评论 0原文

天哪,

我在使用如下所示的 shell 脚本时遇到语法错误:

ssh root@mm-$user-vm-lenny <<EOF

check_dbtag=`grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf`

if [ "$check_dbtag" == "" ]
then
    echo '<<include /home/$user/cvs/dbtag.conf>>' >> /etc/dbtag.conf
fi 
EOF

我收到的错误是

-bash: line 21: syntax error near unexpected token 'newline'
-bash: line 21: 'check_dbtag=<<include /home/thomasw/cvs/dbtag.conf>>'

但是,如果我将行更改

check_dbtag=`grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf`

check_dbtag=`grep '<<include /home/$user/cvs/dbtag.conf>>' /etc/dbtag.conf`

$user 不再进行插值。

如何正确插入变量而不出现任何错误?

G'day,

I'm getting syntax errors when working with a shell script like the following:

ssh root@mm-$user-vm-lenny <<EOF

check_dbtag=`grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf`

if [ "$check_dbtag" == "" ]
then
    echo '<<include /home/$user/cvs/dbtag.conf>>' >> /etc/dbtag.conf
fi 
EOF

The error I'm getting is

-bash: line 21: syntax error near unexpected token 'newline'
-bash: line 21: 'check_dbtag=<<include /home/thomasw/cvs/dbtag.conf>>'

However, I don't get it anymore if I change the line

check_dbtag=`grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf`

to

check_dbtag=`grep '<<include /home/$user/cvs/dbtag.conf>>' /etc/dbtag.conf`

however $user doesn't interpolate anymore.

How can I correctly interpolate the variable without any errors?

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

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

发布评论

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

评论(1

淡淡绿茶香 2024-12-21 09:35:45

反勾号是在您的机器上计算的,而不是在目标机器上计算的。我会这样做:

check_dbtag=\$(grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf)

根据您是否希望在主机或目标计算机上评估 $user ,您可能希望使用 转义 $ \$ 也是如此。

您需要将 $check_dbtag 转义为 \$check_dbtag,因为您希望在目标计算机上对其进行评估。

The back tick is evaluated on your machine, not the target machine. I'd do it like this:

check_dbtag=\$(grep "<<include /home/$user/cvs/dbtag.conf>>" /etc/dbtag.conf)

Depending on whether you want the $user to be evaluated on the host or target machine, you might want to escape $ with \$ as well.

You will need to escape $check_dbtag to \$check_dbtag since you want that to be evaluated on your target machine.

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