Keeping You in the Loop – Bash For, While, Until Loop Examples

Looping statements are used to force a program to repeatedly execute a statement. The executed statement is called the loop body.

Loops execute until the value of a controlling expression is 0. The controlling expression may be any scalar data type.
The shell language also provide several iteration or looping statements. In this article let us review the looping statements which bash provides using some examples.

Loops can be nested. Like any other programming language, bash also supports break statement to exit the current loop, and continue statement to resume the next iteration of the loop statement.

Bash For Loop – First Method

For loops are typically used when the number of iterations is known before entering the bash loop. Bash supports two kinds of for loop. The first form of bash for loop is:

for varname in list
do
commands ##Body of the loop
done

In the above syntax:

for, in, do and done are keywords

List is any list which has list of items

varname is any Bash variable name.

In this form, the for statement executes the command enclosed in a body, once for each item in the list. The current item from the list will be stored in a variable “varname” each time through the loop. This varname can be processed in the body of the loop. This list can be a variable that contains several words separated by spaces. If list is missing in the for statement, then it takes the positional parameter that were passed into the shell.

Bash For Loop Example 1. Unzip all the Zip file

The following example finds the list of files which matches with “*.zip*” in the root directory, and creates a new directory in the same location where the zip file exists, and unzip the zip file content.

In the above code snippet, the for loop generates the random number at max number of times. RANDOM is an internal bash function that returns a random integer at each invocation.

Bash While Loop

Another iteration statement offered by the shell programming language is the while statement.

Syntax:
while expression
do
commands
done

In the above while loop syntax:

while, do, done are keywords

Expression is any expression which returns a scalar value

While statement causes a block of code to be executed while a provided conditional expression is true.

Bash While Example 3. Write contents into a file

The following example reads the data from the stdout and writes into a file.

$ cat writefile.sh
#! /bin/bash
echo -e "Enter absolute path of the file name you want to create"
read file
while read line
do
echo $line >> $file
done
$ sh writefile.sh
Enter absolute path of the file name you want to create
/tmp/a
while
for
until
$ cat /tmp/a
while
for
until

The above example, reads the filename from the user, and reads the lines of data from stdin and appends each line to a given filename. When EOF enters, read will fail, so the loop ends there.

If you are writing lot of bash scripts, you can use Vim editor as a Bash IDE using the Vim bash-support plugin as we discussed earlier.

Bash While Example 4. Read the contents of a file

In the previous example, it reads the data from stdout and write it into a file. In this example, it reads the file
content and write it into a stdout.

$ cat read.sh
#! /bin/bash
echo -e "Enter absolute path of the file name you want to read"
read file
exec <$file # redirects stdin to a file
while read line
do
echo $line
done
$ ./read.sh
Enter absolute path of the file name you want to read
/tmp/a
while
for
until

In this example, it gets the filename to read, and using exec it redirects stdin to a file. From that point on, all stdin comes from that file, rather than from keyboard. read command reads the line from stdin, so while loop reads the stdin, till EOF occurs.

Bash Until Loop

The until statement is very similar in syntax and function to the while statement. The only real difference between the two is that the until statement executes its code block while its conditional expression is false, and the while statement executes its code block while its conditional expression is true.

syntax:
until expression
do
commands #body of the loop
done

In the above bash until syntax:
where until, do, done are keywords
expression any conditional expression

Bash Until Example 5. Monitor the logfile

This example monitors the size of the logfile, once the logfile size reaches 2000bytes, it takes the copy of that logfile.

The until statement continues to execute the body of the loop, till the condition becomes true. In this example condition is size of the file greater than 2000 bytes, so it copies the file once it reaches the 2000 bytes.

That will not read the entire line if there is whitespace at the beginning or end of a line, and it will not read the line exactly as it is in the file if there are escaped characters in the line. The command should be:

while IFS= read -r line

> echo $line

That will not print the line exactly as it was in the file if there are consecutive whitespace characters in the line. Multiple whitespace characters will be replaced by a single space. It should be:

echo “$line”

And even that will not print the line correctly if $line contains, for example, “-n whatever”. When the contents of a variable are unknown, use printf:

Parameter Expansion
…
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
the beginning of the value of parameter, then the result of the
expansion is the expanded value of parameter with the shortest
matching pattern (the ”#” case) or the longest matching
pattern (the ”##” case) deleted. If parameter is @ or *, the
pattern removal operation is applied to each positional
parameter in turn, and the expansion is the resultant list. If
parameter is an array variable subscripted with @ or *, the
pattern removal operation is applied to each member of the array
in turn, and the expansion is the resultant list.

Yes, I will grant you that those are both external commands. I just can’t stand to see people using the awk and sed here and there because, as wonderfull and lovely those commands are, it’s quite overkill for so simple tasks.

Programming well in *sh is related to how many commands you know/understand, and using simple commands to do simple tasks is always good for your karma (and it’s more readable as far as bash is readable).

Otherwise yes, the efficient way to do this replacement is using bash internals, as you suggest.

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.