0

I am accessing a remote machine using the ssh command and a pem file. My code is as follows:

#!/bin/bash
ssh -i rijo.pem [email protected] <<EOF
sudo -s
var=`cat /opt/revsw-config/varnish/sites/rijotests4934567_revsw_net.json | egrep 'SERVER_NAME' | cut -b 19-44`
EOF

When I use this script, I am not able to store the value into the variable but if I run the command in the terminal it works. When I run the script, I get a No such file or directory found error. Can u please help me in this.

3
  • If you are running ./yourscript.sh, try it running by:. ./yourscript.sh Commented Jan 7, 2015 at 10:59
  • no its not working Commented Jan 7, 2015 at 11:18
  • 1
    Wait... how are you using this $var later? What are you trying to do? The lifetime of this variable is very short lived. Commented Jan 7, 2015 at 13:13

1 Answer 1

1

The command

cat /opt/revsw-config/varnish/sites/rijotests4934567_revsw_net.json | egrep 'SERVER_NAME' | cut -b 19-44

is executed locally on your machine, before it is sent over to ssh. This is probably not what you wanted. The "here document" does variable and process substitution so you must escape the backticks to get the desired result.

3
  • ya this command is getting executed but i want to store that value into a variable Commented Jan 7, 2015 at 10:39
  • 1
    The local host will execute the command. The remote host will get something like ˙var=direct result of the command without quotes, possibly newlines...' Unless it is one word, it won't even be a valid syntax and whatever gets into the new line, will get executed as a command (thus "file not found"). It's even dangerous in case a line starts with something that could be a valid command. Put quotes around it and if that doesn't help, consider transferring a file instead. Commented Jan 7, 2015 at 10:49
  • 1
    Or quote the marker for the here-doc: <<"EOF". Commented Jan 7, 2015 at 14:48

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.