shell 脚本中 HERE 文档中的引号
天哪,
我在使用如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反勾号是在您的机器上计算的,而不是在目标机器上计算的。我会这样做:
根据您是否希望在主机或目标计算机上评估
$user
,您可能希望使用转义
也是如此。$
\$您需要将
$check_dbtag
转义为\$check_dbtag
,因为您希望在目标计算机上对其进行评估。The back tick is evaluated on your machine, not the target machine. I'd do it like this:
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.