0% found this document useful (0 votes)
52 views

Ex: 2 Shell Programming Objectives

The document discusses implementing shell programming using commands, substitutions, expansions, functions, patterns and loops. It provides objectives and learning outcomes, then lists several problems that can be solved using shell scripts, such as finding the greatest of three numbers, calculating factorials, and file manipulation. It provides sample code for solving some of the problems, like finding the greatest of three numbers. The document aims to teach students shell programming concepts through practical examples and hands-on practice with problems.

Uploaded by

727822tuit204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Ex: 2 Shell Programming Objectives

The document discusses implementing shell programming using commands, substitutions, expansions, functions, patterns and loops. It provides objectives and learning outcomes, then lists several problems that can be solved using shell scripts, such as finding the greatest of three numbers, calculating factorials, and file manipulation. It provides sample code for solving some of the problems, like finding the greatest of three numbers. The document aims to teach students shell programming concepts through practical examples and hands-on practice with problems.

Uploaded by

727822tuit204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex : 2 SHELL PROGRAMMING

Objectives:

To implement Shell programming using Command syntax, Substitutions, Expansion,


Simple functions, Patterns and Loops.

Learning Outcomes:

After the completion of the experiment, Student will be able to


• Understand the basic Commands in shell.
• Write simple functions, loops, Patterns, Expansions, Substitutions.

Problem Statement:

The program in shell scripting language to find,


 Greatest of three numbers
 Factorial of N Numbers.
 Sum of N numbers.
 Odd or Even Number.
 Fibonacci Series
 Multiplication Table.
 Swapping of Two Numbers.
 File manipulation.
 Palindrome or not
 Positive or negative number.
 Prime number or not.
 Area of different shapes.

Algorithm:

Greatest of three numbers:

• Start the program.


• Enter any three numbers.
• It will check the condition $a -gt $b -a $a -gt $c.
• If a is greater than print a is greater.
• If b is not greater then compare b and c.
• If b is greater than c print b is greater.
• Else print c is greater.
• Stop the program.
• Execute the program.

Execution of the Program:


$ sh filename.sh// Executing the program
Sample Coding:

// Greatest among 3 numbers

echo Enter 3 numbers with spaces in between

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:

Enter 3 numbers with spaces in between

385

Largest of 3 8 5 is 8

Test cases:

Enter Numbers in different ranges.


Include choices for executing the many concepts in one program

Sample Coding:

// Factorial

echo "enter the


number"read n
fact=1
i=1
while [ $i -le $n
]do
fact=`expr $i \*
$fact`i=`expr $i + 1`
done
echo "the factorial number of $ni is $fact
Sample Output:

Enter
the
num
ber :
4
The factorial of 4 is 24.

PROGRAMS:

//addition of two numbers

echo "enter the number"


read a
read b
c=` expr $a + $b `
echo "$c"

//User interaction

read -p "Enter name and branch :" name branch


echo "Welcome $name to $branch department"

//greatest of two numbers

echo "enter the first number"


read a
echo "enter the second number"
read b
if [ $a -gt $b ] ; then
echo "a is greater"
else
echo "b is greater"
fi
//biggest of three numbers

echo "enter the first number"


read a
echo "enter the second number"
read b
echo "enter the third number"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is greater"
elif [ $b -gt $c ]
then
echo "$b is greater"
else
echo "$c is greater"
fi

//swapping of two numbers

echo " enter the first element "


read a
echo "enter the second element"
read b
echo "Before swapping a=$a, b=$b"
temp=$a
a=$b
b=$temp
echo " After swapping the element a=$a ,b=$b"

//odd or even

echo "Enter a number"


read a
w=` expr $a % 2 `
if [ $w -eq 0 ]
then
echo "The number is even"
else
echo "The number is odd"
fi

//fibonacci series

echo "Enter the limit"


read n
echo "fibonacci series"
a=0
b=1
sum=0
echo "$a"
echo "$b"
while [ $sum -le $n ]
do
sum=`expr $a + $b`
echo $sum
a=$b
b=$sum
done

# Number series

echo "Enter the limit"


read n
i=0
echo "The series are : "
while [ $i -le $n ]
do
echo "$i"
i=`expr $i + 1`
done

//sum of series

echo "Enter the limit"


read n
sum=0
i=1
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "SUM : $sum"
//sum of square series

echo "Enter the limit"


read n
i=1
sum=0
while [ $i -le $n ]
do
x=`expr $i \* $i`
i=`expr $i + 1 `
sum=`expr $sum + $x `
done
echo "Sum of Square Series : $sum"

#palindrome

echo "Enter the string"


read str
i=` expr length $str `
cnt=$i
s=``
while [ $cnt -gt 0 ]
do
st=`echo $str|cut -c $cnt`
s=`echo $s$st`
cnt=`expr $cnt - 1`
done
if [ $str = $s ]
then
echo "$str is a palindrome"
else
echo "$str is not a palindrome"
fi

# 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

//Concatenation of two files

echo enter a file


read str1
echo enter second file
read str2
echo $str1
cat $str1>>$str2

//check the given number is Armstrong

echo "Enter the number:."


read n
x=$n
sum=0
while [ $n -gt 0 ]
do
y=`expr $n % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
n=`expr $n / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an Armstrong number.."
else
echo "$x is not an Armstrong number.."
fi

//Program using switch case


echo “enter the day of the week (1-7)
read n
case $n in
1)echo ”1 is Sunday”;;
2)echo ”2 is Monday”;;
3)echo “3.is Tuesday”;;
4)echo ”4 is Wednesday”;;
5)echo “5 is thursday”;;
6)echo ”6 is Friday”;;
7)echo “7 is Saturday”;;
*)echo ” 9 Invalid option”;;
esac

//Program using switch case

read -p "Enter a letter:" l


case "$l" in
A|a|E|e|I|i|o|O|u|U)
echo "Vowel";;
*)
echo "NOT Vowel";;
esac

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

#This will run in bash


n=0
while [ $n -lt 10 ] ; do
echo -n $n
let n=$n+1
done

output:
bash wh.sh
0
1
2
3
4
5
6
7
8
9

Pre lab Questions:

1. How do you find out what’s your shell? - echo $SHELL


2. How do you refer to the arguments passed to a shell script? - $1, $2
and so on. $0 isyour script name.
3. What’s the conditional statement in shell scripting? - if {condition} then … fi
4. What is the use of break command?
5. What is the use of continue command in shell scripting?
6. Tell me the Syntax of “Case statement” in Linux shell scripting?
7. What is the basic syntax of while loop in shell scripting?
8. How to make a shell script executable?
9. What is the use of “#!/bin/bash” ?
10. What is the syntax of for loop in shell script?
11. How to debug a shell script?
12. How compare the strings in shell script?
13. What are the Special Variables set by Bourne shell for command line
arguments?
14. How to test files in a shell script?
15. How to perform arithmetic operation?
16. Write the Basic Syntax of do-while statement?
17. How to define functions in shell scripting?
18. How to use bc (bash calculator) in a shell script?

RESULT:
Thus the implementation of shell program using Command syntax,
Substitutions,Expansion, Simple functions, Patterns and Loops was
implemented successfully.

You might also like