2

I apologize if the title doesn't quite describe what I'm looking for. Please edit if possible.

I have a bash script that does the following:

serveruser="root" serverip=1.2.3.4 serverpath="/var/www" ssh "$serveruser"@"$serverip" /bin/bash <<\EOF mkdir -p "$serverpath/mysqldumps/" cd "$serverpath/mysqldumps/" domainname=somedomain.com mysqldump -h 192.168.1.4 -udba -ppassword -c --add-drop-table --add-locks --create-options --databases --quick --lock-tables $domainname > $domainname.sql EOF 

My problem is that the multiple lines I am "feeding" to ssh don't parse the variables I set before. This is obviously because the variables aren't set on the remote machine, only in my local shell. How could I pass these variables or possibly pass multiple lines to ssh in a different way?

1
  • Why not do it the other way as in echo "cmd $var | ssh user@remote bash? Commented Apr 18, 2013 at 18:26

3 Answers 3

2

The way you're escaping the here-doc word is preventing variable substitution. Contrast

cat <<\END $PATH $LOGNAME END 

versus

cat <<END $PATH $LOGNAME END 

update

On closer inspection, I see you're setting a variable in the heredoc. That should not be expanded on the local machine, so you need to escape those in the mysqldump command. Try this:

ssh "$serveruser"@"$serverip" <<EOF mkdir -p "$serverpath/mysqldumps/" cd "$serverpath/mysqldumps/" domainname=somedomain.com mysqldump -h 192.168.1.4 -udba -ppassword -c --add-drop-table --add-locks --create-options --databases --quick --lock-tables \$domainname > \$domainname.sql EOF 
6
  • For some reason I get strange things happening when I don't use the backslash. For example, my shell seems to pause for over a minute and I get an error later on that mysql can't connect to the server, as if the commands were actually running locally and not on the remote ssh server. I have verified that within the here-doc setting the serverpath var works as expected. I need to be able to set out outside of that here-doc though. Commented Apr 18, 2013 at 18:34
  • 1
    What's happening is that you're logging on to the remote server, running bash non-interactively, and that exits right away. Then the commands in the heredoc get executed. Simply remove "/bin/bash" from the ssh command and it will execute the heredoc remotely. Commented Apr 18, 2013 at 18:41
  • I added /bin/bash to ssh because without it I get the error: Pseudo-terminal will not be allocated because stdin is not a terminal. That error doesn't seem to actually break anything though. Commented Apr 18, 2013 at 18:45
  • Use the ssh options -t or -T to force/disable a pseudo-tty, depending on your needs. That will at least take care of the error message. Commented Apr 18, 2013 at 19:27
  • Thanks for the tip about -T. At first I thought removing /bin/bash solved my problems... but on further testing it's still not expanding the variables within the heredoc. Commented Apr 18, 2013 at 21:36
0
remote_user_name=user instance_ip=127.0.0.1 external=$(ls /home/) ssh -T -i ${private_key} -l ${remote_user_name} ${instance_ip} << END internal=\$(ls /home/) echo "\${internal}" echo "${external}" END 
-1

You need to quote the whole command.. and no need to quote the vars like that put them like ${var}

serveruser="root" serverip=1.2.3.4 serverpath="/var/www" ssh ${serveruser}@${serverip} "/bin/bash <<\EOF mkdir -p ${serverpath}/mysqldumps/ cd ${serverpath}/mysqldumps/ EOF" 
2
  • I always quote just based on info like : mywiki.wooledge.org/BashPitfalls#cp_.24file_.24target . Commented Apr 18, 2013 at 18:14
  • $( ) runs its inner commands in a subshell, so referring to any variable like that doesn't seem to work in bash. Commented Apr 18, 2013 at 18:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.