1

I need to replace a string in a file on a remote server with this:

ssh username@${TARGETSERVER} -i /path/to/ssh-key perl -p -i -e "s#\$user = \'${SDBUSER}\'\;#\$user = \'${TDBUSER}\'\;#g"  ${TARGETDIR}/configuration.php

That would replace the db user on a remote server in a joomla installation, btw.

If I execute that interactively on the remote server with all the variables filled, it IS working.

If I put the above line in a bash script, I get this message and no replacement takes place:

"Substitution pattern not terminated at -e line 1."

Any clue how I can make this work? I've already tried several escapes like \\' and so forth, but I didn't succeed.

Thanks in advance.

3
  • can you try '{' ? like: 's{'"$1"'}{'"$2"'}g' Commented Feb 18, 2015 at 21:10
  • I've tried this: REPLOLD="\$user = \'${SDBUSER}\'\;#"; REPLNEW="\$user = \'${TDBUSER}\'\;#"; ssh username@${TARGETSERVER} -i /path/to/ssh-key perl -p -i -e 's{'"$REPLOLD"'}{'"$REPLNEW"'}g' ${TARGETDIR}/configuration.php .... but didn't work :( ==> Substitution pattern not terminated at -e line 1. Commented Feb 18, 2015 at 21:23
  • @SohamA see solution down below, but thanks anyways Commented Feb 18, 2015 at 21:37

1 Answer 1

1

BINGO - Got it working:

ssh -T username@${TARGETSERVER} -i /path/to/ssh-key <<EOI
  perl -p -i -e "s#\$user = \'${SDBUSER}\'\;#\$user = \'${TDBUSER}\'\;#g"  ${TARGETDIR}/configuration.php
  exit
EOI

From there, I could add several perl commands so I wouldn't have to have the payload of sshing in each time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.