无法在shell脚本中使用EOT



无法在if内使用ssh命令的传输结束(EOT),它会给出编译错误。我试过使用<<-EOT和<<<EOT,但没有任何效果。有人能建议解决这个问题吗?>

#!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

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

EOT标记必须不能缩进。

我刚刚遇到这个问题,删除缩进将解决这个问题:

代替:

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

你可以试试:

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

如果我没记错的话,EOT是带有数字值4的字符。如果您在代码中定义(为了更好的可读性)

eot=$'04'

您可以稍后使用${eot}来表示您的传输结束。

我在这里的回答是指在bash程序中表示传输结束字符的问题。正如AKX在他的评论中合理地指出的那样,真正的问题与传输结束无关,而是如何标记这里文档的结束。