Gaurav Dadhania
Gaurav Dadhania

Reputation: 5327

Quotes within HERE document in shell scripts

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?

Upvotes: 1

Views: 192

Answers (1)

DarkDust
DarkDust

Reputation: 92335

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.

Upvotes: 2

Related Questions