0

Unable to use End of transmission (EOT) with ssh command inside if, it gives compilation error. I have tried using <<-EOT and <<<EOT but nothing worked. Can anyone suggest a fix for this?

#!bin bash

if [ -z "$2" ];
  then
    rsync -a abc.tgz root@$1:/var/folder1
    echo "Done upload"

    # Change permissions of agent image and create image configuration
    ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOT
    chmod 644 /var;
    echo "image.id=$containerSha" > /var;
    EOT
else
    rsync -a abc.tgz root@$1:/var
    echo "Upload done"

    ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOT
    cd /var;
    sshpass -p '$3' rsync -a abc.tgz root@$2:/var;
    sshpass -p '$3' ssh root@$2 'chmod 777 /var/*; ls -ltr';
    EOT
fi
exit
Snehal Gupta
  • 314
  • 1
  • 2
  • 13
  • 1
    Your shebang on first line looks odd to me - you may not even be running `bash` – Mark Setchell Jul 18 '22 at 10:09
  • Per the `bash` tag you used - `For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting here.`. – Ed Morton Jul 18 '22 at 11:47

3 Answers3

3

Running your script through Shellcheck reveals these errors (along with a misshaped shebang line):

Line 12:
    EOT
^-- SC1039 (error): Remove indentation before end token
    (or use <<- and indent with tabs).
 
Line 21:
    EOT
^-- SC1039 (error): Remove indentation before end token
    (or use <<- and indent with tabs).
 
Line 23:
exit
    ^-- SC1072 (error): Here document was not correctly terminated.
    Fix any mentioned problems and try again.

The EOT markers must not be indented.

AKX
  • 152,115
  • 15
  • 115
  • 172
1

I just had this issue and removing indents will solves the problem:

Instead of:

...
    ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOT
    chmod 644 /var;
    echo "image.id=$containerSha" > /var;
    EOT
else
...

You can try:

...
ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOT
chmod 644 /var;
echo "image.id=$containerSha" > /var;
EOT
else
...
huynp
  • 44
  • 4
-3

If I'm not mistaken, an EOT is the character with the numeric value 4. If you define (for better readability) in your code

eot=$'\004'

you can later use ${eot} to denote your end-of-transmission.

UPDATE: My answer here refers to the problem of representing the end-of-transmission character in a bash program. As AKX reasonably argued in his comment, the real question is unrelated to end-of-transmission, but on how to mark the end of a here-document.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • `EOT` here is just the end-of-heredoc marker, it's not related to the character. – AKX Jul 18 '22 at 09:17
  • @AKX: In the **code** the OP does use the textual string `EOT`, but in the **question**, the OP asks explicitly for including and _End of Transmission_, which would be a character. That's why I thought he wants to use an ASCII EOT-code somehow, othewise it would not make sense to talk about _end of transmission_ in the first place. Actually, he can use any charater sequence for his here-document, so why using this specific mentioning of an ASCII control character? – user1934428 Jul 18 '22 at 09:22
  • 1
    I'd guess they just googled EOT (since EOT tends to be in the examples for heredocs) and found that it can stand for End of Transmission. – AKX Jul 18 '22 at 09:25
  • @AKX : Sounds reasonable to me now. I clarified the misunderstanding in a comment. I was thinking to delete my answer, but it still might be helpful if someone searches SO for finding how to encode control characters in bash. – user1934428 Jul 18 '22 at 09:30