dazza2608
dazza2608

Reputation: 27

bash for loop, sequences and then an anomaly

Does anyone know if there is a way to specify a sequence with anomalies thrown in too in a for loop so that i can ssh to a whole heap of machines without typing in each machine number individually?

ie

     for i in comp{1..5,7,9}; do
         ssh root@$i" echo $i; $doOtherStuff";
     done

I hope this isnt too vague - im struggling to find the right words so its possible ive missed a keyword to search for in existing questions

Thanks

Upvotes: 1

Views: 40

Answers (2)

Barmar
Barmar

Reputation: 781255

You can't mix commas and ranges in a single brace. But you can do it with multiple levels:

for i in comp{{1..5},7,9}
    ...
done

Upvotes: 4

Wintermute
Wintermute

Reputation: 44043

I hope I understand what you're trying correctly. If I do, then one way to do it is

 for i in {1..5} 7 9; do
     comp="comp$i"
     ssh root@$comp" echo $comp; $doOtherStuff";
 done

the list i iterates through is 1 2 3 4 5 7 9. You can then use that to assemble the name of the machine (in the variable comp here) and use that the way you intended to use i.

Upvotes: 2

Related Questions