Quotes within HERE document in shell scripts

牧云@^-^@ 提交于 2021-02-17 02:53:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8076183/quotes-within-here-document-in-shell-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!