Bash Scripting – some real time examples of loops
Now that you’ve seen how to use the different ways to create loops in shell scripts, let’s look at some practical examples of how to use them. Looping is a common way to iterate through data on the system, whether it’s files in folders or data contained in a file. Here are a couple of examples that demonstrate using simple loops to work with data.
When you run a program from the command line, the Linux system searches a series of folders looking for that file. Those folders are defined in the PATH environment variable. If you want to find out just what executable files are available on your system for you to use, just scan all the folders in the PATH environment variable. That may take some time to do manually, but it’s a breeze working out a small shell script to do that.
IFS=: for folder in $PATH do
for file in $folder/* do
if [ -x $file ] then echo “ $file” fi
$ cat test25 #!/bin/bash # finding files in the PATH IFS=: for folder in $PATH do echo “$folder:” for file in $folder/* do if [ -x $file ] then echo “ $file” fi done done $
$ ./test25 | more /usr/local/bin: /usr/bin: /usr/bin/Mail /usr/bin/Thunar /usr/bin/X /usr/bin/Xorg /usr/bin/[ /usr/bin/a2p /usr/bin/abiword /usr/bin/ac /usr/bin/activation-client /usr/bin/addr2line …
The goal of shell scripts is to make life easier for the system administrator. If you happen to work in an environment with lots of users, one of the most boring tasks can be creating new user accounts. Fortunately, you can use the while loop to make your job a little easier!
Instead of having to manually enter useradd commands for every new user account you need to create, you can place the new user accounts in a text file and create a simple shell script to do that work for you. The format of the text file that we’ll use looks like this:
The first entry is the userid you want to use for the new user account. The second entry is the full name of the user. The two values are separated by a comma, making this a comma-separated file format, or .csv. This is a very common file format used in spreadsheets, so you can easily create the user account list in a spreadsheet program and save it in .csv format for your shell script to read and process.
To read the file data, we’re going to use a little shell scripting trick. We’ll actually set the IFS separator character to a comma as the test part of the while statement. Then to read the individual lines, we’ll use the read command. That looks like this:
while IFS=‘,’ read –r userid name
The read command does the work of moving onto the next line of text in the .csv text file, so we don’t need another loop to do that. Thewhile command exits when the read command returns a FALSE value, which happens when it runs out of lines to read in the file. Tricky!
Putting everything together results in this script:
$ cat test26 #!/bin/bash # process new user accounts input=“users.csv” while IFS=‘,’ read -r userid name do echo “adding $userid” useradd -c “$name” -m $userid done < “$input” $
# ./test26 adding rich adding christine adding barbara adding tim #
# tail /etc/passwd rich:x:1001:1001:Richard Blum:/home/rich:/bin/bash christine:x:1002:1002:Christine Bresnahan:/home/christine:/bin/bash barbara:x:1003:1003:Barbara Blum:/home/barbara:/bin/bash tim:x:1004:1004:Timothy Bresnahan:/home/tim:/bin/bash #