System Administrator 101 — A Basic Bash Loop — Part 1 “for”
Being a System Administrator you will find that you need to do a lot of repetitive tasks. Moving files, grepping files, and creating users. You could spend all your time type the same commands over and over, but why?
You can save a lot of time by looping commands. There are three ways you can do that, by using one of the following; for, while or until. We will first focus on for, it my favorite of the three.
The thing to remember with a ‘for loop’ is the following syntax
for variable in x y z ; do command $variable ; done
Variable is what ever you want to call it. Most people use i as a variable, but again it can be anything you want. The x y x is the information that you want to pass to the command. The last thing to remember with a loop is when it passed everything to the command that if finish, that is why you need the done.
Here is an example of a simple count down that you can do with for a ‘for loop’.
for i in 10 9 8 7 6 5 4 3 2 1 ; do echo $i ; done
As you can see, we have set our variable to be i, in that variable we are going to pass 10 to 1 to the command echo. By the way, you can do this another way if you are using Bash 4.0, by changing the number to {10…1} , this because of inbuilt support for setting up a step value use {START..END..INCREMENT} .
Here is another example, we are going fun and make the computer count out load to 100 by 5′s…
for x in {5..100..5} ; do echo $x | festival --tts ; done
In this loop we set our variable to x, and we told it to start with 5 go to 100 by increments of 5. We then passed that on to echo that is piped into festival to read it out load, once it had reach 100, it stops.
Here are some real world examples of how System Administrator use a ‘for loop’. Let say you have a text file that you keep a list of Linux servers that you take care and you need to push a rpm or a file to them.
linux01 192.168.1.1
linux05 192.168.1.5
linux10 192.168.1.10
linux20 192.168.1.20
With a ‘for loop’ it is very simple and fast. We are going to cat the file so that we know what server to access. We are also going to use awk so that we get the ip from the second coloumn. . To get the information we will need to pipe ‘ | “ the loop into the awk command to get it, because we are run commands to get the information for the loop we will need to use ` ` in order to run the statement.
for f in `cat linux_server | awk '{ print $2}'` ; do scp somefile user@$f:/dir ; done
This loop tell that our variable f is going to pass the four ip so that we can scp the file to those boxes.
NOTE::
If you are not sure about what the output going to look like, it always best to use the following to see what the output going to be….
echo $f
Now lets say that file you pushed to the server is rpm that you need to update your server. You can do ‘for loop’, using ssh and pass the command to the boxes to update that rpm
for r in `cat linux_server | awk '{ print $2 }` ; do ssh user@$r rpm -Uvh /dir/myfile ; done
To ensure that the file was updated, you can do another ‘for loop’ check the rpm database to see if there by doing rpm -qa and pipe into a grep with the file name.
for r in `cat linux_server | awk '{ print $2 }` ; do ssh user@$r rpm -qa | grep myfile ; done
By the way you can use all of these commands together in one script to save even more time.
—————-Begin Scripts————————
#!/bin/bash
servers=”linux_server”
files=”files_to_push”# Here we are going push files
for f in `cat $servers | awk '{ print $2}'` ; do scp $files user@$f:/dir ; done
# Here we are going to install the files
for r in `cat $servers | awk '{ print $2 }` ; do ssh user@$r rpm -Uvh /dir/$files ; done
# Here we are going to check if it got installed
for x in `cat $servers | awk '{ print $2 }` ; do ssh user@$x rpm-qa | grep $files ; done
exit 0
—————-Begin Scripts————————
As great System Administrator, finding the simplest ways to do recurring tasks frees up your time for tackling more complex problems . A ‘for loop’ is a great way to help. Next time I will go over while.
–Pup


” cat $file | awk ‘{ print $2}’ ” is actually unecessary … awk can open files itself. Try ” awk ‘{ print $2 }’ $file ” instead.
Doesn’t make a big difference but is a good thing to remember because it applies to other tools too like sed and grep; cat is very often overused
That’s good to know. I was doing from a beginner view, thanks.