EOF到远程主机上的文件

时间:2014-03-08 20:05:40

标签: bash shell sh eof

我需要通过ssh将shell脚本中的文本“cat”到远程机器上。 为简单起见,这是必需的,因此我不需要保留额外的文件。

例如

#!/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

2 个答案:

答案 0 :(得分:2)

如果你在运行时生成文件内容,请尝试这样的事情:

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

如果文件是静态的,只需使用scp。

答案 1 :(得分:1)

只想为大家注意。上面的示例适用于纯文本,但如果heredoc包含变量,那么将进行替换。用单引号保护heredoc短语以保持一切完整是非常重要的 例如

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

主变量不会被实际主机名替换。 作为参考(第19-7段)http://tldp.org/LDP/abs/html/here-docs.html