Professional Documents
Culture Documents
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
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
commands
done
Syntax 2:-
for ((initialization; condition; increment/decrement))
do
commands
done
Example:-
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.
i=1
do
echo "$i"
i=$(( $i + 1 ))
done
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:
green
yellow
Syntax of select:
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 "***********************"
done
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 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