How to set heredoc output to local variable
7,358
You need to quote the EOF
marker, eg <<\EOF
or <<'EOF'
to stop
your $filename
variable from being evaluated before it is passed to the remote. You can see the effect with say /bin/bash -v
instead of /bin/bash
.
I also needed to have the actual EOF
marker on a line of its own, with the final )
on the next line.
Related videos on Youtube
Author by
Jokas
Updated on September 18, 2022Comments
-
Jokas over 1 year
I'm trying in a heredoc to set its output to a local variable as follows :
REMOTE_OUTPUT=$(ssh remote@server /bin/bash << EOF find my/path/ -type f -not -path my/path/*/ -type f -mtime -0 | while read filename; do if grep "ERROR" $filename; then filenamebase=$(basename "$filename") echo -e "\n----------------------------------------------------------\n\n$filenamebase failure:\n" grep -n "ERROR" "$filename" | sed G fi done EOF)
But the variable stays null even though the find&grep loop is correct and should indeed return an output.
(Otherwise I would also be interested in writing the output of the heredoc into a local file.)
-
RobertL over 8 yearsWhat's that mean: "find&grep loop is correct"? How do you know?
-
Jokas over 8 yearsI mean I have tried it locally to check that the output is not empty (same folders and files in the local and remote servers)
-
RobertL over 8 yearsDid you try with
ssh
, but without the command substitutionREMOTE_OUTPUT=$( )
? -
Jokas over 8 yearsYes it works fine but I need to store the output to append it to a file whose content will be sent in a mail
-
Jokas over 8 yearsNothing is append to the file
-
Dmitry Grigoryev over 8 yearsWhat happens if you run your command with here-doc but without command substitution? Also, have you tried to run
ssh
with-T
option? -
RobertL over 8 yearsDid the command go back to the prompt or did you have to interrupt it with control-C?
-
Jokas over 8 yearsMeuh found the solution below :)
-
RobertL over 8 yearsDid you try the
>somefile
without the$()
? -
RobertL over 8 yearsDid you delete the
ssh
too? -
RobertL over 8 yearsDid you try everything in the answer below?
-
-
RobertL over 8 yearsI think the problem is the EOF) because questioner said the command worked without the command substitution $().
-
Jokas over 8 yearsI've done it, it is it that solved my issue :)