0

I have the below script in 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 command makes use of a variable from the above commands. But it doesn't seem to be working. What I get an error is mv: cannot move '/etc/letsencrypt/live/autotest.domain.com' to a subdirectory of itself, '/etc/letsencrypt/live/autotest.domain.com/autotest.domain.com' Even set hostname also didnt seem to work (especially with variables), when I checked the other server to see if hostname is changed. Tried many things around variable like adding "" and {} etc, but nothing worked.

Can someone help me on this regard.

serverstackqns
  • 722
  • 2
  • 16
  • 39

1 Answers1

2

Shell variables are NOT expanded in single-quoted ' text. Use double quotes " for strings with shell variables.

Tomek
  • 2,950
  • 1
  • 15
  • 9
  • Commenting with the right answer: 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" – serverstackqns Apr 08 '22 at 07:33