【问题标题】:EOF to file on remote hostEOF 到远程主机上的文件
【发布时间】:2014-03-08 20:05:40
【问题描述】:

我需要通过 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

【问题讨论】:

    标签: bash shell sh eof


    【解决方案1】:

    如果您在运行时生成文件内容,请尝试这样的操作:

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

    如果文件是静态的,或者简单地使用 scp。

    【讨论】:

    • 可以省略- - 足够的cat &gt; /somefile :)
    • 是的,它确实适用于测试样本,谢谢!但是我的真实的东西给出了错误-cat:/tmp/viewgpu:没有这样的文件或目录脚本是pastebin.com/zkNNdsg4所以heredoc中的脚本开始在本地机器上执行(这不是必需的),只需将内容传输到远程PC
    【解决方案2】:

    只想给大家注意一下。上面的示例适用于纯文本,但如果 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

    【讨论】:

    • 如果您使用 bash,最好使用 $(uname -a) 而不是 `uname -a`。在你的问题中,你没有说任何关于“heredoc”内部的“变量”......第三,你可能是指示例代码中的echo $host......
    猜你喜欢
    • 2017-09-09
    • 2019-12-31
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2017-08-27
    • 2017-01-15
    相关资源
    最近更新 更多