我正在尝试使用以下命令通过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-receive
和post-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/