0

Im trying to execute 2 commands in 12 different servers using a loop. When i try to do it remotly my sequence fails and it just iterate using i=12. Do I need some scape character?

ssh 10.10.10.10 "for i in {01..12}; do modpkg -e -n host-prv$i; runpkg -n host-prv$i; done"

UPDATE - worked after using single quotes - thanks @dave_thompson_085

ssh 10.10.10.10 'for i in {01..12}; do modpkg -e -n host-prv$i; runpkg -n host-prv$i; done'

Luiz Bom
  • 25
  • 4
  • Yu are using double quotes. This means that `$i` will be expanded by the time you issue the ssh command, and not when the loop is executed. – user1934428 Aug 24 '22 at 08:10
  • ... and the easiest way to avoid that is to use _single_ quotes (aka apostrophes) `' ... '` if you can, and here you can. In more complicated cases with multi-level quoting, if you need the outer quotes to be doublequotes, you can use backslash `\$...` to prevent parameter expansion (also command substitution and arithmetic expansion) at the source system. – dave_thompson_085 Aug 24 '22 at 09:11

1 Answers1

0

user1934428's comment here is one solution, but you could also refactor the command and move the loop to run in the local shell, will the side effect of having to run ssh for each iteration:

for i in {01..12}; do ssh 10.10.10.10 "modpkg -e -n host-prv$i; runpkg -n host-prv$i"; done
MikeK
  • 702
  • 4
  • 11