You are on page 1of 5

Chapter - VI

 Return codes and values


 Looping Mechanisms
1. The for loop
2. The while loop
3. The until loop
 The select construct
 true and false statements
 break and continue statements

Return Codes and values:-

Reading user input with read and getting return back of the values.

In many ocations you may want to prompt the user for some input, and there are several ways
to archive this. This is one of those ways:

#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
Output:-
Andrews
Hi Andrews!

As a variant, you can get multiple values with read, this example may clarify this.

#!/bin/bash
echo Please, enter your firstname and lastname
read FN LN
echo "Hi! $LN, $FN !"
Output:-
Abc DEF
Hi! Abc, DEF

The for command

The for command iterates over the elements of a specified list:

Syntax 1:-
for variable in list
do

commands

done
Page 1 of 5
Within the commands, you can reference the current element of the list by means of the shell
variable $ variable, where variable is the name specified following the for. The list typically
takes the form of a series of arguments, which can incorporate metacharacters. For example,
the following for command:

Example:-
for i in 2 4 6 8
do
echo $i
done

prints the numbers 2, 4, 6, and 8 on successive lines.

for variable in list


do

commands

done

Syntax 2:-
for ((initialization; condition; increment/decrement))
do

commands

done

Example:-

for ((i=1; i<=5; i++))


do

echo “value of i=$i”

done

Output:-

value of i=1
value of i=2
value of i=3
value of i=4
value of i=5

Page 2 of 5
The while command

The while command lets you execute a series of commands iteratively (that is, repeatedly) so
long as a condition tests true:

while condition
do
commands
done

Here's a script that uses a while command to print its arguments on successive lines:

echo $1
while shift 2> /dev/null
do
echo $1
done

The commands that comprise the do part of a while (or another loop command) can include if
commands, case commands, and even other while commands. However, scripts rapidly
become difficult to understand when this occurs often. You should include conditional
commands within other conditional commands only with due consideration for the clarity of
the result. Include a comment command (#) to clarify difficult constructs.

Example for While:-

i=1

while [ "$i" -le 10 ]

do

echo "$i"

i=$(( $i + 1 ))

done

The until command

The until command lets you execute a series of commands iteratively (that is, repeatedly) so
long as a condition tests false:

Syntax:-

until command
Page 3 of 5
do
commands
done

Here's a script that uses an until command to print its arguments on successive lines, until it
encounters an argument that has the value red:

Example:-
until test $1 = red
do
echo $1
shift
done

For example, if the script were named stopandgo and stored in the current working directory,
the command:

./stopandgo green yellow red blue

would print the lines:

green
yellow

The Select Construct:-

Select and Case Statement

Similar to the "switch/case" construct in C programming, the combination of "select" and


"case" provides shell programmers with the same features. The "select" statement is not part
of the "case" statement, but I've put the two of them together to illustrate how both can be
used in programming.

Syntax of select:

select <variable> in <list>


do
...statements...
done

Syntax of case:

case $<variable> in
<option1>) statements ;;
<option2>) statements ;;
*) echo "Sorry, wrong option" ;;
esac

Page 4 of 5
The example below will explain the usage of select and case together, and display options
involving a machine's services needing to be restarted. When the user selects a particular
option, the script starts the corresponding service.

#!/bin/bash

echo "***********************"
select opt in apache named sendmail
do
case $opt in
apache) /etc/rc.d/init.d/httpd restart;;
named) /etc/rc.d/init.d/named restart;;
sendmail) /etc/rc.d/init.d/sendmail restart;;
*) echo "Nothing will be restarted"
esac
echo "***********************"

# If this break is not here, then we won't get a shell prompt.


break

done
True and False Statements:-

If given condition is true then command1 is executed otherwise(False) command2 is


executed. The IF loop is the statement that works with the true and false statements.

Syntax:
if condition
then
condition is zero (true - 0)
execute all commands up to else statement

else
if condition is not true then
execute all commands up to fi
fi

The break and continue commands

The break and continue commands are simple commands that take no arguments. When the
shell encounters a break command, it immediately exits the body of the enclosing loop
( while, until, or for) command. When the shell encounters a continue command, it
immediately discontinues the current iteration of the loop. If the loop condition permits, other
iterations may occur; otherwise the loop is exited.

Page 5 of 5

You might also like