Bash repetitive tasks ( for , while , until )

Bash repetitive tasks ( for , while , until )

To run a specific task or list of commands repetitively on bash script , you need to use on of this three constructions : for , while , until . Each one fits different situation as we will know now.

The for loop

The for loop is the first of the three shell looping constructs. This loop allows for specification of a list of values. A list of commands is executed for each value in the list.

The syntax for this loop is:

for NAME in [ LIST ]; do COMMANDS; done

NAME can be any variable name, although i is used very often. LIST can be any list of words, strings or numbers, which can be literal or generated by any command. The COMMANDS to execute can also be any operating system commands, script, program or shell statement. The first time through the loop, NAME is set to the first item in LIST. The second time, its value is set to the second item in the list, and so on. The loop terminates when NAME has taken on each of the values from LIST and no items are left in LIST.

Example :

To list all file in /etc/ that ends with .conf and have file type of ASCII :

Th3-Gam3 ~ # for i in 'ls /etc/*.conf'; do file /etc/$i | grep ASCII; done
/etc/adduser.conf:         ASCII text
/etc/apg.conf:             ASCII text
/etc/casper.conf:          ASCII text
/etc/debconf.conf:         ASCII text
/etc/deluser.conf:         ASCII text
/etc/fuse.conf:            ASCII text
/etc/fwupd.conf:           ASCII text
/etc/gai.conf:             ASCII text
/etc/hdparm.conf:          ASCII text
/etc/host.conf:            ASCII text
  • you can create a variable to hold the resutls  List=”$(ls /etc/*.conf)” results and use it in for loop like that ” for i in “$List”; do … “.

The while loop

The while construct allows for repetitive execution of a list of commands, as long as the command controlling the while loop executes successfully (exit status of zero). The syntax is:

while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done

CONTROL-COMMAND can be any command(s) that can exit with a success or failure status. The CONSEQUENT-COMMANDS can be any program, script or shell construct.

As soon as the CONTROL-COMMAND fails, the loop exits. In a script, the command following the done statement is executed.

The return status is the exit status of the last CONSEQUENT-COMMANDS command, or zero if none was executed.

Example:

#!/bin/bash

# This script opens 4 terminal windows.

i="0"

while [ $i -lt 4 ]
do
xterm &
i=$[$i+1]
done

The until loop

The until loop is very similar to the while loop, except that the loop executes until the TEST-COMMAND executes successfully. As long as this command fails, the loop continues. The syntax is the same as for the while loop:

until TEST-COMMAND; do CONSEQUENT-COMMANDS; done

The return status is the exit status of the last command executed in the CONSEQUENT-COMMANDS list, or zero if none was executed. TEST-COMMAND can, again, be any command that can exit with a success or failure status, and CONSEQUENT-COMMANDS can be any UNIX command, script or shell construct.

As we already explained previously, the “;” may be replaced with one or more newlines wherever it appears.

Break and continue

Bash repetitive tasks ( for , while , until )

The break built-in

The break statement is used to exit the current loop before its normal ending. This is done when you don’t know in advance how many times the loop will have to execute, for instance because it is dependent on user input.

Bash repetitive tasks ( for , while , until )

The continue built-in

The continue statement resumes iteration of an enclosing for, while, until or select loop.

When used in a for loop, the controlling variable takes on the value of the next element in the list. When used in a while or until construct, on the other hand, execution resumes with TEST-COMMAND at the top of the loop.

Example :

#!/bin/bash

# This script suppose to print numbers from 0 to 6 ,
# skipping 2 ,but it will stop after 3 becasue we used break.

i="0"

while [ $i -lt 7 ]
do

if [[ $i -eq 2 ]]; then
i=$[$i+1]
continue


elif [[ $i -eq 4 ]] ; then
echo "The loop is breaked at 4 !!"
break

else
echo $i
fi

i=$[$i+1]

done

To run it and see :

 ~ # bash while.sh 
0
1
3
The loop is breaked at 4 !!

You now should be able to use for , while , until loop constructions and use break and continue to control the flow of loop.

That is it, i hope it was simple, thanks for joining me.
Enjoy !.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s