Scripting Language LOOP AND FUNCTIONS



Objective:
·         Learn how to work with expressions in Shell Scripting
·         Learn about conditional structures in Shell
·         Learn about functions in Shell
·         Hands on Practice on different shell scripts using case and fucntions

2: Expressions
If you are going to assign the output of any expression to a variable in shell, then it can be done in the following way: -
var1= $((num1+num2))
       or
var1= `expr $num1 + $num2`
we use the keyword expr in writing expressions.
3: Control Structures
The flow of control within SH scripts is done via four main constructs;
1.      if...then...elif..else
2.      do...while
3.      for
4.      case
3.1: if...then...elif..else
This construct takes the following generic form,
if [condition]
then statement
elif [condition]
then statement...
else statement
fi

Conditionals let us decide whether to perform an action or not, this decision is taken by evaluating an expression.
Expressions
An expression can be: String comparison, Numeric comparison, File operators and Logical operators and it is represented by [expression]:
Numerical Comparison
Symbol
Description
Usage
-eq
Equals to
[$A -eq $B]
-ne
Not Equal to
[$A -ne $B]
-lt
Less than
[$A -lt $B]
-le
Less than and equal to
[$A -le $B]
-gt
Greater than
[$A -gt $B]
-ge
Greater than and equal to
[$A -ge $B]
String Comparison
Symbol
Description
Usage
=
Equals to
[$A = $B]
!=
Not Equal to
[$A != $B]
-n
Not a null string
[-n $A ]
-z
Null string
[-z $A]

Testing For File
Symbol
Description
Usage
-r
Read Permission
[-r $filename]
-w
Write Permission
[-w $filename]
-x
Execution Permission
[-x $filename]
-f
File Exists
[-f $filename]
-d
Directory Exits
[-d $filename]
-s
Size of File is not zero
[-s $filename]





Logical Operators
Symbol
Description
Usage
!
This is logical negation. This inverts a true condition into false and vice versa.
[ ! false ]
-o
This is logical OR. If one of the operands is true, then the condition becomes true.
[ $a -lt 20 -o $b -gt 100 ]
-a
This is logical AND. If both the operands are true, then the condition becomes true otherwise false.
[ $a -lt 20 -a $b -gt 100 ]

Example
echo "Enter numnber : "
read n
rem=$(( $n % 2 ))
 if [ $rem -eq 0 ]
 then echo "$n is even number"
else
  echo "$n is odd number"
fi
3.2  Do .. While
The Do...While takes the following generic form:
Syntax
while [condition]
do statement
done
Example
count=1
while [ $count –le 10]
do
           
echo $count
count=`expr $count + 1`
done
3.3 For
The syntax of the command is:
Syntax 1
for i in {START..END..INCREMENT}
do statement
done
Example
for i in {1..10..2}
do
            echo $i
done
Syntax 2
for ((Expression1; Expression2; Expression3))
do statement
done
Example
for ((i=0; i<5; i++))
do
            echo $i
done
3: Practice Exercise
Q1: Write a Shell Script to determine largest among three integers and compare them with each other.
Q2: Write a Shell Script to determine a given year is leap year or not.
Q3: Write a Shell Script to verify that specific file in current location has read permission or not.




Case Statement:
Case is used for multiple choices in shell. The syntax for that is
case variable/expression in
pattern) statement(s) ;;
...
esac
Example:
echo "Enter a Number"
read r
case $r in
1 ) echo "First Choice"
;;
2) echo "Second Choice"
;;
*) echo "Any Other"
;;
esac
If you want to check a range the pattern will be [1-9] or [a-z] and if you want to enter a string in pattern it should be in double quotes.
3: Functions
Functions works in shell similar like any other programming language. First its definition and then its calling. The syntax for the function definition is as follows: -
function_name () {
   list of commands}


Example 1
Hello () {
   echo "Hello World"
}
Hello











Example 2
sum () {
n=$(($1 + $2))
return $n
}
echo "enter 2 numbers"
read n1
read n2
sum $n1 $n2
ans=$?
echo $ans












Q1: Write a Shell Script in which enter a number from 1-7 and will display the corresponding day on that use case. For example, if user entered 1 then day will be Monday and so on.
Q2: Write a Shell Script that define a function named check. A number will be passed to the function as an argument and the function will check whether the number is even or odd or zero.

Post a Comment

0 Comments