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.
Finding executable files
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.
The first step is to create a for loop to iterate through the folders stored in the PATH environment variable. When you do that, don’t forget to set the IFS separator character:
IFS=:
for folder in $PATH
do
Now that you have the individual folders in the $folder variable, you can use another for loop to iterate through all the files inside that particular folder:
for file in $folder/*
do
The last step is to check whether the individual files have the executable permission set, which you can do using the if-then test feature:
if [ -x $file ]
then
echo “ $file”
fi
And there you have it! Putting all the pieces together into a script looks like this:
$ 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
$
When you run the code, you get a listing of the executable files that you can use from the command line:
$ ./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 output shows all the executable files found in all the folders defined in the PATH environment variable, which is quite a few!
Creating multiple user accounts
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:
userid,user name
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!
To feed the data from the file into the while command, you just use a redirection symbol at the end of the while command.
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”
$
The $input variable points to the data file and is used as the redirect data for the while command. The users.csv file looks like this:
$ cat users.csv
rich,Richard Blum
christine,Christine Bresnahan
barbara,Barbara Blum
tim,Timothy Bresnahan
$
To run the problem, you must be the root user account, because the useradd command requires root privileges:
# ./test26
adding rich
adding christine
adding barbara
adding tim
#
Then by taking a quick look at the /etc/passwd file, you can see that the accounts have been created:
# 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
#
Congratulations, you’ve saved yourself lots of time in adding user accounts!
Dear Admin,
we are very happy with your detailed descriptions on each topics kindly post a good article about these topics if possible
* kick start – NFS/HTTP/FTP/PXE and trouble shooting
*MY SQL/oracle – INSTALLATION/Backup n Restore/TROUBLE SHOOTING/DB PASSWORD RECOVERY etc..
* using cpio and dd
* backup tricks using find/grep with ctime and erasing unwanted data using rm with ctime [date based deletion/moving]
some interesting techniques if any about monitoring and clustering