Ex: 2 Shell Programming Objectives
Ex: 2 Shell Programming Objectives
Objectives:
Learning Outcomes:
Problem Statement:
Algorithm:
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Largest of $a $b $c is $l
Sample Output:
385
Largest of 3 8 5 is 8
Test cases:
Sample Coding:
// Factorial
Enter
the
num
ber :
4
The factorial of 4 is 24.
PROGRAMS:
//User interaction
//odd or even
//fibonacci series
# Number series
//sum of series
#palindrome
# Arithmetic operation
echo "Enter two numbers"
read a
read b
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read op
case $op in
1)c=` expr $a + $b `;;
2)c=` expr $a - $b `;;
3)c=` expr $a \* $b `;;
4)c=`expr $a / $b`;;
5)exit
esac
echo $c
//positive or negative
echo "Enter a number"
read n
if [ $n -gt 0 ]
then
echo "Positive"
elif [ $n -eq 0 ]
then
echo "Zero"
else
echo "Negative"
fi
//Prime numbers
n=1
echo $n
while [ $n -le 300 ]
do
i=2
while [ $i -le `expr $n - 1` ]
do
if [ `expr $n % $i` -eq 0 ]
then
break
else
i=`expr $i + 1`
fi
done
if [ $n -eq $i ]
then
echo $n
fi
n=`expr $n + 1`
done
//Compare two strings
echo “ enter string 1”
read s1
echo “enter string2”
read s2
if [ $s1 == $s2]
then
echo “ the given string are equal”
else
echo “The given strings are not equal”
fi
user@administrator-ThinkCentre-M72e:~$ sh cas.sh
letter:e
Vowel
user@administrator-ThinkCentre-M72e:~$ sh cas.sh
Enter a letter:r
NOT Vowel
String Equality
Aim:
To find the position of the matched string using shell program.
Program
echo enter the string
read str1
echo enter the sub string
read str2
let len1=${#str}-1
let b=0
let c=0
while [ $c != $len1 ]
do
if [ $str2 == ${str1:$c:2} ]
then
let d=$c
let d=$d+1
echo position matched at $d
break
else
let b=$b+1
fi
let c=$c+1
done
let d=$d+1
if [ $b == $len1 ]
then
echo not match with string
fi
Output:
[test@redhat ~]$ sh substr.sh
enter the string
welcome
enter the sub string
me
position matched at 6
# Script to create simple menus and take action according to that selected
# menu item
#
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0 ;;
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esca
done
Looping
#Executes only in bash
l=10
for((a=1;a<=l;a++));do
echo "$a"
done
output:
bash for1.sh
1
2
3
4
5
6
7
8
9
10
output:
bash wh.sh
0
1
2
3
4
5
6
7
8
9
RESULT:
Thus the implementation of shell program using Command syntax,
Substitutions,Expansion, Simple functions, Patterns and Loops was
implemented successfully.