wrufesh
wrufesh

Reputation: 1406

Cannot create a file remotely via SSH using cat and <<EOF

I am trying to create a file remotely via ssh with the command as follows:

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

After it is successfully executed when I check the file with cat repo.git/hooks/post-receive on remote server I see the following result:

#!/bin/bash
git checkout -f
EOF
chmod +x hooks/post-receive

I expect EOF and chmod +x hooks/post-receive not to be present in post-receive file. What can be done to solve this.

Upvotes: 1

Views: 945

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

From 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.

So, you need to remove trailing spaces from your here document or substitute them with tabs.

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"

Upvotes: 1

Related Questions