Home > Mobile >  how to concatenate/assemble the alias name of some bash cli commands
how to concatenate/assemble the alias name of some bash cli commands

Time:11-30

I have the following aliases in .bash_aliases

ssh51ro="ssh -p12345 [email protected]" 
ssh52ro="ssh -p12345 [email protected]"
ssh53ro="ssh -p12345 [email protected]"
...
ssh59ro="ssh -p12345 [email protected]"

I wonder if I can send the same command to multiple recipients in a for loop. I would normally increment an "i" variable and concatenate the name of the alias together.

for i in $( seq 1 9 ) ; do ssh5${i}ro date; done

But this doesn't seem to work. it says specifically that "ssh5iro command is not found".

How can I assemble and run the commands in this case

CodePudding user response:

Do not store commands in variables. Use functions.

myssh() { ssh -p12345 root@"$@"; }
ssh51ro() { myssh 192.168.15.71 "$@"; }
ssh52ro() { myssh 192.168.8.25 "$@"; }
...
for i in $( seq 1 9 ) ; do ssh5${i}ro date; done
# maybe you want dynamic list of functions based on naming pattern:
for func in $(compgen -A function | grep ssh5.ro); do "$func" date; done

You may want to research Ansible and Puppet Bolt.

  • Related