For iterative operations, the bash shell uses three types of loops: for, while, and until. Using the for looping command, we can execute a set of commands for a finite number of times for every item in a list. In the for loop command, the user-defined variable is specified. After the in command, the keyword list of values can be specified. The user-defined variable will get the value from that list and all statements between do and done get executed until it reaches the end of the list.
The purpose of the for loop is to process a list of elements. It has the following syntax:
for variable in element1 element2 element3 do commands done
The simple script with the for loop could be as follows:
for command in clear date cal do sleep 1 $command Done
In the preceding script, the commands clear, date, and cal will be called one after another. The sleep command will be called before every command for a second.
If we need to loop continuously or infinitely, then the following is the syntax:
for ((;;))
do
command
doneLet's write a simple script for_01.sh. In this script, we will print the var variable 10 times:
#!/bin/bash
for var in {1..10}
do
echo $var
doneLet's test the program:
$ chmod +x sor_01.sh $ ./for_01.sh
The following will be the output after executing the preceding commands:
1 2 3 4 5 6 7 8 9 10
The following script for_02.sh uses the C programming style syntax:
#!/bin/bash max=10 for ((i=1; i<=max; i++)) do echo -n "$i " # one case with echo without –n option done
$ chmod +x for_02.sh $ ./for_02.sh
The following will be the output after executing the preceding commands:
$ ./for_02.sh # OUTPUT with –n option 1 2 3 4 5 6 7 8 9 10 $ ./for_02.sh # OUTPUT without –n option 1 2 3 4 5 6 7 8 9 10
In the next script for_03.sh, we will be processing a list of numbers, which are listed next to the in keyword:
#!/bin/bash for var in 11 12 13 14 15 16 17 18 19 20 do echo $var done
Let's test the program:
$ chmod +x for_03.sh $ ./for_03.sh
The following will be the output after executing the preceding commands:
$ ./for_03.sh 11 12 13 14 15 16 17 18 19 20
In the following script for_04.sh, we create users11 to user20 along with their home directory:
#!/bin/bash
for var in user{11..20}
do
useradd –m $var
passwd -d $var
doneLet's test the program:
$ chmod +x for_04.sh.sh $ sudo ./for_04.sh
The following will be the output after executing the preceding commands:
user11 to user20 will be created with their home folders in the /home/ folder. You need to be a root user or administrator to run this script.
In the for_05.sh script, we will be passing command-line parameters. All the command-line parameters will be available as the $* inside script:
#!/bin/sh for var in $* do echo "command line contains: $var" done
Let's test the program:
$ chmod +x for_05.sh $ ./for_05.sh 1 2 3 4 5 6
The following will be the output after executing the preceding commands:
command line contains: 1 command line contains: 2 command line contains: 3 command line contains: 4 command line contains: 5 command line contains: 6
In the next script for_06.sh, we are passing a list of words such as name of fruits. Inside the script, we are printing the information of variable:
#!/bin/bash
# create fruits.txt => Apple Mango Grapes Pears Banana Orange Pineapple
for var in `cat fruits.txt`
do
echo "var contains: $var"
doneLet's test the program:
$ chmod +x for_06.sh $ ./for_06.sh
The following will be the output after executing the preceding commands:
var contains: Apple var contains: Mango var contains: Grapes var contains: Pears var contains: Banana var contains: Orange var contains: Pineapple
Using the for_07.sh script, we generate a list of files with the ls shell command. This will be the list of filenames. In the for loop, the following list of files will be printed:
#!/bin/bash
echo -n "Commands in bin directory are : $var"
for var in $(ls /bin/*)
do
echo -n -e "$var \t"
doneLet's test the program:
$ chmod +x for_07.sh $ ./for_07
The following will be the output after executing the preceding commands:
This will print the content of /bin/ directory.
For taking a backup of files, we can write the for_08.sh script as follows:
#!/bin/bash for filename in *.c do echo "Copying $filename to $filename.bak" cp $filename $filename.bak done
Let's test the program:
$ chmod +x for_08.sh $ touch 1.c 2.c $ ./for_08.sh
The following will be the output after executing the preceding commands:
"Copying 1.c to 1.c.bak" "Copying 2.c to 2.c.bak"