Bash Shell Script: Command Line Foreach Loop

A quick code to demonstrate how you can make use of shell script foreach loop in command line to run any command repeatedly.

[code language=”bash”]
gunnalag@gmd:/u/gunnalag> foreach user ( `cat my-users.txt` )
foreach? ypmatch $user passwd >> my-users-ypmatch.txt
foreach? end
Can’t match key user9 in map passwd.byname. Reason: no such key in map.
Can’t match key root in map passwd.byname. Reason: no such key in map.
gunnalag@gmd:/u/gunnalag>

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *