2

I have a sed command in a bash script like this:

sed -i 's/db.url=.*/db.url='$URL'/' config.properties

and URL var is assigned as:

$URL=jdbc\:oracle\:thin\:@\/\/hostname\:12345\/XYZ

When I run this bash script on the host it exists on it work as intended, replacing the url with the one specified in URL. However, when I add a command in the bash script to do this on my other host like this:

ssh user@host02 <<EOF
 sed -i 's/db.url=.*/db.url='$URL'/' config.properties
exit
EOF

I get this error:

sed: -e expression #1, char 47: unknown option to `s'

Anyone know what may be going on here?

gjw80
  • 1,058
  • 4
  • 18
  • 36

2 Answers2

2

You've properly quote the sed expression if it were running on the local host, but the string is then passed to the shell on the remote host, where the * is now unquoted and expanded as a glob. The simplest thing to do is to pipe the command to the remote shell via standard input, so you don't have to worry about quoting:

echo "sed -i 's/db.url=.*/db.url=$URL/' config.properties" | ssh user@host02 bash

With multiple commands, you may consider using a here document:

ssh user@host02 bash <<EOF
command1
sed -i 's/db.url=.*/db.url=$URL/' config.properities
command2
EOF
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Could I just quote the command inside the ssh? I have many commands being called besides this one and obviously don't want to pipe every one, I also don't want to make this a standalone command. – gjw80 Jun 10 '13 at 15:59
  • You may also want to consider simply `scp`ing a locally written script and executing that remotely via `ssh`. Trying to get the quoting right will be tricky at best, impossible at worst. A here document may also be useful. – chepner Jun 10 '13 at 16:01
  • I already do use a here doc, as indicated in my original example – gjw80 Jun 10 '13 at 16:04
  • also, I do scp the file over to the other host, I use ssh to run the bash script where it gets moved to. – gjw80 Jun 10 '13 at 16:07
  • The difference is, I am explicitly running a `bash` instance on the remote end. – chepner Jun 10 '13 at 16:10
  • I added the bash to the command and it's still giving same error – gjw80 Jun 10 '13 at 16:15
1

The solution was to use double single quotes (') around $URL like this: ''$URL''

gjw80
  • 1,058
  • 4
  • 18
  • 36