shell 腳本中 set-hostname 和 mv 指令的變數

shell 腳本中 set-hostname 和 mv 指令的變數

我在 Shell 中有以下腳本:

read n
for ((i=1;i<=$n;i++))
do
echo "Connecting to $publicip"
ssh -i ./key.txt root@$publicip 'hostnamectl set-hostname autotest$i.domain.com && mv /etc/letsencrypt/live/autotest.domain.com /etc/letsencrypt/live/autotest$i.domain.com && reboot'
done

mv 指令使用上述指令中的變數。但它似乎不起作用。我收到的錯誤是mv: 無法將'/etc/letsencrypt/live/autotest.domain.com' 移動到其自身的子目錄'/etc/letsencrypt/live/autotest.domain.com/autotest.domain.com '當我檢查其他伺服器以查看主機名稱是否已更改時,即使設定主機名稱似乎也不起作用(特別是對於變數)。圍繞變數嘗試了很多事情,例如添加“”和{}等,但沒有任何效果。

有人可以在這方面幫助我嗎?

答案1

Shell 變數不會在單引號'文字中展開。對帶有 shell 變數的字串使用雙引號"

相關內容