Score:0

Variables on set-hostname and mv command in shell script

cn flag

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.

Score:1
fr flag

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

serverstackqns avatar
cn flag
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"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.