124-Linux Shell Scripting
124-Linux Shell Scripting
Session 04
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
Structured
Commands (while)
Exercise : Write a script that asks a yes/no question from user and
does not continue until yes or no is entered.
answer=""
while [ "$answer" != "yes" ] && [ "$answer" != "no" ]
do
echo "Do you want to exit ? "
read answer
done
Controlling the
Loop
The break command
The break command is a simple way to escape out of a loop in
progress. You can use the break command to exit out of any type of
loop, including for and while.
#!/bin/bash
echo "The first parameter is $1"
#!/bin/bash
fact=1
i=1
while [ $i -le $1 ]
do
fact=$[ $fact * $i ]
i=$[ $i + 1 ]
done
echo "The factorial is $fact"
$ ./factorial.sh 5
Handling User
Input
Testing parameters
You need to be careful when using command line parameters in your
shell scripts. If the script runs without the parameters, bad things
can happen.
-n is a string operator that checks if a string is not null.
-z is a string operator that check if a string is null.
#!/bin/bash
if [ -n "$1" ]
then
echo "The first parameter is $1"
else
echo "First parameter is not entered, exiting …"
exit 1
fi
Handling User
Input
Special Parameter Variables
Counting parameters
The special $# variable contains the number of command line
parameters included when the script was run. You can use this
special variable anywhere in the script, just as a normal variable:
#!/bin/bash
echo There were $# parameters supplied.
Handling User
Input
So you have the ability to test the number of parameters present
before trying to use them:
#!/bin/bash
if [ $# -ne 2 ]
then
echo Usage: script_name a b
else
…….
fi
Handling User
Input
Grabbing all the data
There are situations where you’ll want to just grab all of the
parameters provided on the command line and iterate through all of
them.
#!/bin/bash
echo "Input parameters using method 1 : $*"
echo "Input parameters using method 2: $@"
Handling User
Input
#!/bin/bash
count=1
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[ $count + 1 ]
done
echo "##########"
count=1
for param in "$@"
do
echo "\$@ Parameter #$count = $param"
count=$[ $count + 1 ]
done
Handling User
Input
Shifting Parameters
#!/bin/bash
# extracting command line options as parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option";;
esac
shift
done
Handling User
Input
Processing options with values - Using the getopts command
The optstring defines the valid option letters used in the command
line. It also defines which option letters require a parameter value.
Handling User
Input
The OPTARG environment variable contains the value to be used if
an option requires a parameter value.
Option Description
-a Show all objects
-f Specify a file to read data from
-h Display a help message for the command
-n Do nothing, just display what will happen
-o Specify an output file to redirect all output to
-q Run in quiet mode
-r Process directories and files recursively
-s Run in silent mode
-v Produce verbose output
-y Answer yes to all questions
Handling User
Input
Getting User Input - Basic reading
The read command accepts input from the standard input (the
keyboard) and places the data into a standard variable
#!/bin/bash
echo -n "Enter your name: "
read name
echo "Hello $name"
Tip: If you need to use another value after variable without space
you need to use braces around variable name :
item=A
echo "This is ${item}1"
Handling User
Input
The read command includes the -p option, which allows you to
specify a prompt directly in the read command line:
You can use the -t option specify a timer. The -t option specifies the
number of seconds for the read command to wait for input. When
the timer expires, the read command returns a non-zero exit status:
if read -t 5 -p "Please enter your name: " name ; then
echo "Hello $name, welcome to my script"
else
echo
echo "Sorry, too slow!"
fi
Handling User
Input
Silent reading
There are times when you need input from the script user, but you
don’t want that input to display on the monitor. One example could
be when entering passwords.
The -s option prevents the data entered in the read command from
being displayed on the monitor (actually, the data is displayed, but
the read command sets the text color to the same as the
background color).
Example:
#!/bin/bash
count=1
cat /etc/passwd | while read line
do
echo "Line $count: $line"
count=$[ $count + 1]
done
Handling User
Input
Example 2:
#!/bin/bash
count=1
while read line
do
echo "Line $count: $line"
count=$[ $count + 1]
done < /etc/passwd