bash - 无法使用cat和<< EOF通过SSH远程创建文件

标签 bash ssh

我正在尝试使用以下命令通过ssh远程创建文件:

ssh $REMOTE_USER@$REMOTE_HOST "
    cat > hooks/post-receive <<EOF
    #!/bin/bash
    git checkout -f
    EOF
    chmod +x hooks/post-receive
"

当我在远程服务器上使用cat repo.git/hooks/post-receive检查文件成功执行之后,我看到以下结果:
#!/bin/bash
git checkout -f
EOF
chmod +x hooks/post-receive

我希望EOF文件中不存在chmod +x hooks/post-receivepost-receive
如何解决这个问题。

最佳答案

man bash:

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen.

...

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.



因此,您需要从here文档中删除尾随空格或用制表符代替它们。
ssh $REMOTE_USER@$REMOTE_HOST "
cat > hooks/post-receive <<EOF
#!/bin/bash
git checkout -f
EOF
chmod +x hooks/post-receive"

# or,

ssh $REMOTE_USER@$REMOTE_HOST "
cat > hooks/post-receive <<-EOF
    #!/bin/bash
    git checkout -f
    EOF
    chmod +x hooks/post-receive"

关于bash - 无法使用cat和<< EOF通过SSH远程创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920170/

相关文章:

python - 如何从字符串中的paramiko获取私钥?

bash - 通过 SSH 执行存储在文件中的 Bash 脚本

linux - Cygwin 狂欢 : read file word by word

bash - 在Circleci中使用golang-ci-lint

bash - 带有子 shell 的 'cd' 命令的别名未按预期工作

bash - bash echo 命令可以返回非零退出代码吗?

php - 如何在CentOS 8下安装phpseclib?

amazon-web-services - AWS : How to manage instance ppk or pem files?

ubuntu - 尝试从 github 克隆到谷歌云计算引擎上的虚拟机

linux - Bash:检测所有磁盘并显示可用空间