Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
If you are running ./yourscript.sh, try it running by:. ./yourscript.sh –  Tejas Jan 7 at 10:59
    
no its not working –  Rijo Mon Jan 7 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. –  orion Jan 7 at 13:13

1 Answer 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.

share|improve this answer
    
ya this command is getting executed but i want to store that value into a variable –  Rijo Mon Jan 7 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. –  orion Jan 7 at 10:49
1  
Or quote the marker for the here-doc: <<"EOF". –  muru Jan 7 at 14:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.