Bash For Loop
Bash Scripting for loop examples
The for loop can be used to repeat a given set of instructions over and over again. It can be used to read a list of items that need to be processed. These items can be names, file names, numbers, variables. The basic syntax of a for loop is as follows:
Syntax of a for loop:
#!/bin/bash for i in item1 item2 item3 item4 item5 do echo $i done
In the above example, our list is read into the variable $i one item at a time. The code between the "do" and "done" statement is executed for each item read into $i. For example, the output below is from the above script:
Output from above for loop script
john@john-desktop:~/scripts$ ./for1.sh item1 item2 item3 item4 item5
for loop using the contents of a variable
As we saw in the previous example, our list is read into the variable $i. Instead of passing a list, we could have passed a variable that contained a list:
#!/bin/bash file_list="/home/john/scripts/test1.txt /home/john/scripts/test2.txt /home/john/scripts/test3.txt" for files in $file_list do [ -f $files ] && echo "file: $files exists" || echo "file: $files is missing" done
In the above example a variable $file_list has been given a list of files. This list is read one at a time into a variable called $files. We then carry out a test for the presence of the file using the file object test "[ -f $files ]. If the file exists we execute the echo statement "file....exists". If the file doesn't exist we then execute the "files...is missing" statement.
Output from the above script
john@john-desktop:~/scripts$ ./for2.sh file: /home/john/scripts/test1.txt exists file: /home/john/scripts/test2.txt is missing file: /home/john/scripts/test3.txt exists
From the above we can see that two of the files have been found and one of the files is missing.
Processing the contents of a file using a for loop
Another useful way a "for loop" can be used is to process the contents of a file. In the example that follows, I have created a simple script that creates new users that are listed within a file. This can be useful if you have numerous users to create.
Useradd script with for loop
#!/bin/bash echo "Running Script as user $USER " count=0 for i in $(cat ./userlist.txt) do echo "Adding user $i" useradd -m -c "$i" $i echo "Return code: $?" ((count++)) echo "Record processed are $count" done echo "Last lines form /etc/passwd follow" tail -n $count /etc/passwd
The above script starts by displaying who has executed the script. This information is held within the variable $USER
The variable $count is set to a value of "0" initially as we will be using this variable to keep count of how many users have been added from the file.
The line for i in $(cat ./userlist.txt) uses command substitution to read the contents of the file userlist.txt.
The "do....done section contains the commands that are to be executed against each entry that was read from the file userlist.txt. Here we use the "useradd" command to create new users. The return code from this command is displayed by the echo "Return code: $?" line. Our running count variable $count is incremented by 1 to keep track of how many records are processed. The last line tail -n $count /etc/passwd displays the associated entries in the passwd file for the new users.
Contents of userlist.txt
root@john-desktop:/home/john/scripts# cat userlist.txt test1 test2 test3 root@john-desktop:/home/john/scripts# cat for3.sh
Output from the above script
root@john-desktop:/home/john/scripts# ./for3.sh Running Script as user root Adding user test1 Return code: 0 Record processed are 1 Adding user test2 Return code: 0 Record processed are 2 Adding user test3 Return code: 0 Record processed are 3 Last lines form /etc/passwd follow test1:x:1003:1003:test1:/home/test1:/bin/sh test2:x:1004:1004:test2:/home/test2:/bin/sh test3:x:1005:1005:test3:/home/test3:/bin/sh