简体   繁体   English

如果在 shell 脚本中,则无法在内部使用 EOT

[英]Unable to use EOT inside if in shell script

Unable to use End of transmission (EOT) with ssh command inside if, it gives compilation error.无法在 if 中使用带有 ssh 命令的传输结束 (EOT),它会给出编译错误。 I have tried using <<-EOT and <<<EOT but nothing worked.我曾尝试使用 <<-EOT 和 <<<EOT 但没有任何效果。 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

Running your script through Shellcheck reveals these errors (along with a misshaped shebang line):通过Shellcheck运行脚本会显示这些错误(以及形状不正确的 shebang 行):

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. EOT标记不得缩进。

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

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 是数值为 4 的字符。如果您在代码中定义(为了更好的可读性)

eot=$'\004'

you can later use ${eot} to denote your end-of-transmission.您可以稍后使用${eot}来表示您的传输结束。

UPDATE : My answer here refers to the problem of representing the end-of-transmission character in a bash program.更新:我在这里的回答是指在 bash 程序中表示传输结束字符的问题。 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.正如 AKX 在他的评论中合理论证的那样,真正的问题与传输结束无关,而是关于如何标记此处文档的结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM