Name: Hemant Chandrashekhar Borase
Roll no: 9 Class:MCA-I
Sub: Shell Script Program
Program 1: Simple shell script echo and read
echo "What is your name"
read name
echo "Hello, $name"
output:
What is your name
Hemant
Hello, Hemant
Program 2: Simple shell script echo and assign
Name=" Hemant Borase "
class="MCA"
Rollno=45
echo "$Name : $class"
Output:
Hemant Borase : MCA
Program 3: To understand special var and to get data from
command line argument
echo "File name: $0"
echo "Current Process id:$$"
echo "First parameter: $1"
echo "Second parameter: $2"
echo "Quoted values @: $@"
echo "Quoted values *: $*"
echo "Total number of parameters#: $#"
output:
File name: ./[Link]
Current Process id:3879
First parameter: 2
Second parameter: 4
Quoted values @: 2 4 Hemant
Quoted values *: 2 4 Hemant
Total number of parameters#: 3
Program 4: Expression evaluation based on command line arg
val=`expr $1 + $2`
echo "Addition of two num is :$val"
val1=`expr $1 - $2`
echo "Subtraction of two num is:$val1"
val2=`expr "$1 * $2"| bc`
echo "Multiplication of two num is: $val2"
val3=`expr $1 / $2`
echo "Division of two num is: $val3"
val4=`expr $1 % $2`
echo "Modulo division of two num is:$val4"
Output:
Addition of two num is :8
Subtraction of two num is:4
Multiplication of two num is:12
Division of two num is:3
Modulo division of two num is:0
Program 5: Shell script to create calculator based on user choice
i="y"
sum=0
echo "enter first no"
read a
echo "enter second no"
read b
while [ $i = "y" ]
do
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "enter the choice"
read ch
case $ch in
1)sum=`expr $a + $b`
echo "addition is $sum";;
2)sum=`expr $a - $b`
echo "subtraction is $sum";;
3)sum=`expr $a \* $b`
echo "multiplication is $sum";;
4)sum=`expr $a / $b`
echo "division is $sum";;
5)sum=`expr $a % $b`
echo "modulo division is $sum";;
esac
echo "do you want to continue"
read i
if [ $i != "y" ]
then
exit
fi
done
Output:
enter first no
6
enter second no
10
[Link]
[Link]
[Link]
[Link]
[Link]
enter the choice
1
addition is 16
do you want to continue
y
[Link]
[Link]
[Link]
[Link]
[Link]
enter the choice
2
subtraction is -4
do you want to continue
y
[Link]
[Link]
[Link]
[Link]
[Link]
enter the choice
3
multiplication is 60
do you want to continue
y
[Link]
[Link]
[Link]
[Link]
[Link]
enter the choice
4
division is 0
do you want to continue
y
[Link]
[Link]
[Link]
[Link]
[Link]
enter the choice
5
modulo division is 6
do you want to continue
n
Program 6: Shell script program to greet the user with good
morning ,good afternoon,good evening as user log in
hour=$(date +"%H")
if [ $hour -ge 0 -a $hour -lt 12 ]
then
greet="good morning, $USER"
elif [ $hour -ge 12 -a $hour -lt 18]
then
greet="good afternoon, $USER"
else
greet="Good evening, $USER"
fi
echo $greet
Output:
Good evening, student
Program 7: Shell script program to find largest of three numbers
echo "enter the three number "
read a b c
l=$a
if [ $l -lt $b ]
then
l=$b
fi
if [ $l -lt $c ]
then
l=$c
fi
echo "largest is $l"
Output:
enter the three number
589
largest is 9
Program 8: Shell script program to find gcd of two number
echo "enter the two number "
read a b
m=$a
if [ $b -lt $m ]
then
m=$b
fi
while [ $m -ne 0 ]
do
x=`expr $a % $m`
y=`expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
echo "gcd of $a and $b is $m"
break
fi
m=`expr $m - 1`
done
Output:
enter the two number
64
gcd of 6 and 4 is 2
Program 9: Shell script program to find reverse of given number
echo "enter the any number "
read n
rev=0
rem=0
temp=$n
while [ $temp -ne 0 ]
do
rem=`expr $n % 10`
rev=`expr $rev \* 10 + $rem`
temp=`expr $temp \/ 10`
done
echo "reverse of number is $rev"
Output:
enter the any number
78
reverse of number is 87
Program 10: Shell Script program to find givent number is
palindrome or not
echo "enter the any number "
read n
rev=0
rem=0
temp=$n
while [ $temp -ne 0 ]
do
rem=`expr $temp % 10`
rev=`expr $rev \* 10 + $rem`
temp=`expr $temp \/ 10`
done
echo "reverse of number is $rev"
if [ $rev -eq $n ]
then
echo "$n is palindrome"
else
echo "$n is not palindrome"
fi
Output:
enter the any number
89
reverse of number is 98
89 is not palindrome
Program 11: Shell Script program to find prime number in range
echo "enter the two number "
read a b
st=$a
while [ $st -le $b ]
do
no=$st
i=1
c=0
while [ $i -le $no ]
do
ans=`expr $no % $i`
if [ $ans -eq 0 ]
then
c=`expr $c + 1`
fi
i=`expr $i + 1`
done
if [ $c -eq 2 ]
then
echo "$no"
fi
st=`expr $st + 1`
done
Output:
enter the two number
2 10
2
3
5
7
Program 12: Shell script program to count words ,lines and special
symbols
echo "enter the text"
read text
li=`expr $text | wc -l`
w=`expr $text | wc -w`
c=`expr $text | wc -c`
sp=$(echo "$text" | grep -o '[^a-z A-Z 0-9]' | wc -l)
echo "No's of lines is $li"
echo "No's of words is $w"
echo "No's of character is $c"
echo "No's of special symbol is $sp"
Output:
enter the text
hello,world!
No's of lines is 1
No's of words is 1
No's of character is 13
No's of special symbol is 2
Program 13: Shell script program to count characters ,words and
lines from file
echo enter the filename
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo number of characters in $file is $c
echo number of words in $file is $w
echo number of lines in $file is $l
Output:
enter the filename
[Link]
number of characters in [Link] is 16
number of words in [Link] is 3
number of lines in [Link] is 1
Program 14: Shell Script program that demonstrate the while loop
#example of whlie loop
a=0
while [ "$a" -lt 10 ]
do
b="$a"
while [ "$b" -ge 0 ]
do
echo -n "$b"
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
Output:
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
Program 15: Shell script program for array handling
week[0]="Monday"
week[1]="Tuesday"
week[2]="wednesday"
week[3]="thursday"
week[4]="friday"
week[5]="saturday"
week[6]="sunday"
echo "first index: ${week[0]}"
echo "second index: ${week[1]}"
echo "first method : ${week[*]}"
echo "second method: ${week[@]}"
Output:
first index: Monday
second index: Tuesday
first method : Monday Tuesday wednesday thursday friday saturday sunday
second method: Monday Tuesday wednesday thursday friday saturday sunday
Program 16: Shell script program for string compairing
str1="computer"
str2="department"
if [ $str1 = $str2 ]
then
echo "strings are same"
else
echo "strings are not same"
fi
output:
strings are not same
Program 17: Shell script program for string compairing
#string handling compare
echo "enter the first string "
read str1
echo "enter the second string"
read str2
if [ $str1 \< $str2 ]
then
echo "$str1 is less than $str2"
else
echo "$str1 not less than $str2"
fi
output:
enter the first string
dev
enter the second string
sonar
dev is less than sonar
Program 18: Shell script program for checking whether string is
empty or not
#check string is empty or not
echo "enter the first string"
read str1
echo "enter the second string"
read str2
if [ -n $str1 ]
then
echo "string 1 is not empty"
else
echo "string 1 is empty"
fi
if [ -n $str2 ]
then
echo "string2 is not empty"
else
echo "string 2 is empty"
fi
Output:
enter the first string
dev
enter the second string
sonar
string 1 is not empty
string2 is not empty
Program 19: Shell script program for string assignment
#string assignment
var1="welcome"
echo $val1
var2="to school"
echo ${var2}
var3="computer science"
echo "$var3"
var=${var1}${var2}${var3}
echo $var
var=$var1$var2$var3
echo "$var"
Output:
to school
computer science
welcometo schoolcomputer science
welcometo schoolcomputer science
Program 20: Use of break in for loop
for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done
Output:
10
15
Program 21: Use of continue in for loop
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "the $NUM is an even number"
continue
fi
echo "the $NUM found is odd number"
done
Output:
the 1 found is odd number
the 2 is an even number
the 3 found is odd number
the 4 is an even number
the 5 found is odd number
the 6 is an even number
the 7 found is odd number
Program 22: Shell script program for finding postion of substring in
main string
read -p "Enter main string: " main_string
read -p "Enter substring: " sub_string
# Use parameter expansion to find the position
position=${main_string%%$sub_string*}
if [ "$position" == "$main_string" ]; then
echo "Substring not found"
else
echo "Position of '$sub_string' in '$main_string': $((${#position} + 1))"
fi
Output:
Enter the main string :Devyani Sonar
Enter the substring: Sonar
Position of 'Sonar' in 'Devyani Sonar': 9
Program 22: Shell script program for calculating sum of digit
#sum of digits
echo "enter the input no"
read num
sum=0
rem=0
while [ $num -ne 0 ]
do
rem=`expr $num % 10`
sum=`expr $sum + $rem`
num=`expr $num / 10`
done
echo "sum is $sum"
Output:
enter the input no
23
sum is 5