I am accessing remote machine in Linux using Bash scripting.My code is as follows

 ssh -i manu_bp.pem ubuntu@ <<EOF
 sudo -s
 cat /opt/revsw-config/apache/rijomon2_revsw_net/ui-config.json > r1.txt
 echo `$(jq ".rev_component_co.mode" r1.txt)`

I am not able to print the jq value. If I write this command as echo $(jq '.rev_component_co.mode' r1.txt) in the terminal of the remote machine, it's showing the exact result. But if I write it in a script and try to execute, it shows the error

jq: r1.txt: No such file or directory
  • I fixed presentation, however the line with sudo, might be sudo -c cat .. – Archemar Jan 21 '15 at 7:46
  • no its not working – Rijo Mon Jan 21 '15 at 7:49
  • 1
    if jq a programm ? the line echo $(jq ".rev_component_co.mode" r1.txt) means: run jq ".rev_component_co.mode" r1.txt , then run the result, then echo it. This is not what you want I guess. – Archemar Jan 21 '15 at 7:58
  • You might also provide us with error message, either in comment or by editing the post (there might be a small delay while poste beeing approved) – Archemar Jan 21 '15 at 7:59
  • if i write this command as echo $(jq '.rev_component_co.mode' r1.txt) in terminal of remote machine its showing the exact result but if i write in script and try to execute its showing error as "jq: r1.txt: No such file or directory". – Rijo Mon Jan 21 '15 at 9:11

You're running jq on the local machine, since you're using an “unquoted” here document, where the characters \`$ are interpolated. To run the command in the remote shell, arrange for the text passed in the here document to contain the command. Since you don't use any local variable or command substitution, the easiest way to do that is to use a literal here document.

ssh -i manu_bp.pem ubuntu@ <<'EOF'
sudo -s
cat /opt/revsw-config/apache/rijomon2_revsw_net/ui-config.json > r1.txt
echo "$(jq ".rev_component_co.mode" r1.txt)"

I assume that using backquotes around dollar-parenthesis was a mistake — that would take the output of the jq command and use that output in turn as a command to execute.

You can simplify this: echo "$(somecommand)" is a convoluted way of printing the output of somecommand (with a few differences, such as stripping off final newlines and in some shells doing backslash expansion, but if these matter at all then using echo is probably the wrong method).

ssh -i manu_bp.pem ubuntu@ <<'EOF'
sudo -s
cat /opt/revsw-config/apache/rijomon2_revsw_net/ui-config.json > r1.txt
jq ".rev_component_co.mode" r1.txt

You could just write this in one command (newlines optional), but beware that you need two levels of quoting, one for the local shell and one for the remove shell.

ssh -i manu_bp.pem ubuntu@ 'sudo sh -c "
  cat /opt/revsw-config/apache/rijomon2_revsw_net/ui-config.json > r1.txt;
  jq .rev_component_co.mode r1.txt"'

If you don't need to run jq as root, only to read the file as root, use

ssh -i manu_bp.pem ubuntu@ '
  sudo cat /opt/revsw-config/apache/rijomon2_revsw_net/ui-config.json > r1.txt;
  jq .rev_component_co.mode r1.txt'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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