Demontager
Demontager

Reputation: 227

EOF to file on remote host

I need to "cat" a text inside shell script to remote machine via ssh. This is required for simplicity, so i don't need to keep extra file.

For instance

#!/bin/sh
VAR="some"
VAR1="something"

cat << EOF
apple
green
tree
EOF   ---> cat to file.txt on remote machine

do some command
do some command1
exit 0

Upvotes: 1

Views: 3603

Answers (2)

Demontager
Demontager

Reputation: 227

Just want to note for everyone. Above example works fine for plain text, but if heredoc includes variables then substitution will take place. So important to protect heredoc phrase with single quotes to keep everything intact For example

cat <<'EOF' | ssh remote 'cat - > /tmp/my_remote_file.txt'
host=$(uname -a)
echo $host
EOF

host variable won't be substituted with actual hostname. As reference (paragraph 19-7) http://tldp.org/LDP/abs/html/here-docs.html

Upvotes: 1

taro
taro

Reputation: 5832

Try something like this if you're generating file content in runtime:

cat <<EOF | ssh remote 'cat - > /tmp/my_remote_file.txt'
apple
green
tree
EOF

Or simply use scp if file is static.

Upvotes: 3

Related Questions