Shell脚本中HERE文档中的引号 [英] Quotes within HERE document in shell scripts

查看:66
本文介绍了Shell脚本中HERE文档中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day,

使用如下所示的shell脚本时出现语法错误:

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

我得到的错误是

-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`

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

但是 $ user 不再插值了.

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

How can I correctly interpolate the variable without any errors?

推荐答案

反勾号是在您的计算机而不是目标计算机上评估的.我会这样:

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)

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

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

您需要将 $ check_dbtag 转义为 \ $ check_dbtag ,因为您希望在目标计算机上评估那个.

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

这篇关于Shell脚本中HERE文档中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆