簡體   English   中英

如果在 shell 腳本中,則無法在內部使用 EOT

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

無法在 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=$'\004'

您可以稍后使用${eot}來表示您的傳輸結束。

更新:我在這里的回答是指在 bash 程序中表示傳輸結束字符的問題。 正如 AKX 在他的評論中合理論證的那樣,真正的問題與傳輸結束無關,而是關於如何標記此處文檔的結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM