Home > front end >  Unable to use EOT inside if in shell script
Unable to use EOT inside if in shell script

Time:07-18

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related