Thursday, August 26, 2010

unix shell programming

# Program to count no of occurence of vowel charaters
echo Enter String
read string
Len=`expr length $string`
count=0
i=1
while [ $i -le $Len ]
do
char=`expr substr $string $i 1`
if [ $char = a -o $char = e -o $char = i -o $char = o -o $char = u ]
then count=`expr $count + 1`
fi
i=`expr $i + 1`
done
echo Vowel Occurence = $count
Output
——
Enter String
jishnu
Vowel Occurence = 2



# Program to count number of occurance of a character in a string

echo "Enter String"
read string
echo "Enter Character"
read char
count=0
i=1
len=`expr length $string`
echo "String Length : $len"
while [ $i -le $len ]
do
cchar=`expr substr $string $i 1`
if [ $cchar = $char ]
then
count=`expr $count + 1`
fi
i=`expr $i + 1`
done
echo "No of Occurence of '$char' : $count"

Output
——-

Enter String
asdasasdasd
Enter Character
a
String Length : 11
No of Occurence of 'a' : 4
# Program to find grade,score & modulus of marks using 5 marks of a student
echo "Enter 5 Marks"
read m1 m2 m3 m4 m5
total=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo "Total Marks = $total"
percent=`expr $total \* 100 / 500`
echo "Percentage of Marks = $percent"
if [ $percent -lt 40 ]
then
echo "D Grade"
elif [ $percent -ge 40 -a $percent -lt 65 ]
then
echo "C Grade"
elif [ $percent -ge 65 -a $percent -lt 75 ]
then
echo "B Grade"
elif [ $percent -ge 75 -a $percent -lt 90 ]
then
echo "A Grade"
else
echo "A++ Grade"
fi
OUTPUT
——
Enter 5 Marks
67 98 89 78 89
Total Marks = 421
Percentage of Marks = 84
A Grade
Enter 5 Marks
56 67 45 63 45
Total Marks = 276
Percentage of Marks = 55
C Grade
# Program to find fibonacci numbers within a given range
echo Enter Limit
read n
f1=0
f2=1
echo $f1
echo $f2
n=`expr $n – 2`
while [ $n -ne 0 ]
do
f3=`expr $f1 + $f2`
f1=$f2
f2=$f3
echo $f3
n=`expr $n – 1`
done
OUTPUT
——
Enter Limit
6
0
1
1
2
3
5
# Menu driven program to perform arithmetic operations
echo "Enter 2 Numbers"
read a b
opt=0
while [ $opt -ne 6 ]
do
echo "Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit"
read opt
case $opt in
1)
echo "$a +"
echo "$b"
echo "—-"
echo $a + $b|bc;;
2)
echo "$a -"
echo "$b"
echo "—-"
echo $a – $b|bc;;
3)
echo "$a *"
echo "$b"
echo "—-"
echo $a \* $b|bc;;
4)
echo "$a /"
echo "$b"
echo "—-"
echo $a / $b|bc;;
5)
echo "$a %"
echo "$b"
echo "—-"
echo $a % $b|bc;;
6)
;;
*)
echo "wrong option"
esac
done
OUTPUT
——
Enter 2 Numbers
4.4 2.2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
1
4.4 +
2.2
—-
6.6
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
2
4.4 -
2.2
—-
2.2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
3
4.4 *
2.2
—-
9.6
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
4
4.4 /
2.2
—-
2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
5
4.4 %
2.2
—-
0
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
6
# Program to check whether given number is armstrong number or not
echo "Enter Number"
read num
sum=0
item=$num
while [ $item -ne 0 ]
do
rem=`expr $item % 10`
cube=`expr $rem \* $rem \* $rem`
sum=`expr $sum + $cube`
item=`expr $item / 10`
done
if [ $sum -eq $num ]
then
echo "$num is an Amstrong Number"
else
echo "$num is not an Amstrong Number"
fi
OUTPUT
——
Enter Number
123
123 is not an Amstrong Number
Enter Number
1
1 is an Amstrong Number
Enter Number
153
153 is an Amstrong Number


# shell program to create a menu which displays
# the output for the curresponding options
# 1. Current Directory
# 2. Todays date
# 3. List of users logged in
echo Menu
echo 1. Current Directory
echo 2. Todays date
echo 3. List of users logged in
echo enter your choice :
read opt
case $opt in
1)
echo "Current Directory"
pwd;;
2)
echo "Todays Date"
date;;
3)
echo "Users Logged in"
who;;
*)
echo "wrong option"
esac

Output
————
Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
1
Current Directory
/rhome/it07022/s4/pe

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
2
Todays Date
Mon Feb 16 11:06:05 EST 2009

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
3
Users Logged in
it07022 :0 2009-02-16 10:09
it07022 pts/0 2009-02-16 10:42 (:0.0)

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
4
wrong option
# Shell program to find sum of squares of 10 numbers
sum=0
i=0
while [ $i -ne 10 ]
do
echo "Enter Number"
read num
sum=`expr $sum + $num \* $num`
i=`expr $i + 1`
done
echo Sum of squares of numbers = $sum
Output
——-
Enter Number
1
Enter Number
2
Enter Number
3
Enter Number
4
Enter Number
5
Enter Number
6
Enter Number
7
Enter Number
8
Enter Number
9
Enter Number
10
Sum of squares of numbers = 385


# Shell program to find sum of odd numbers in a series of 10 numbers
sum=0
i=0
while [ $i -ne 10 ]
do
echo "Enter Number"
read num
if [ `expr $num % 2` -ne 0 ]
then
sum=`expr $sum + $num`
fi
i=`expr $i + 1`
done
echo Sum of odd numbers = $sum

Output
————-

Enter Number
1
Enter Number
2
Enter Number
3
Enter Number
4
Enter Number
6
Enter Number
8
Enter Number
3
Enter Number
1
Enter Number
1
Enter Number
1
Sum of odd numbers = 10


# Shell program to reverse a given number
echo "Enter Number"
read num
num2=0
while [ $num -ne 0 ]
do
num2=`expr $num % 10 + $num2 \* 10`
num=`expr $num / 10`
done
echo $num2

Output
————

Enter Number
1234
4321

Enter Number
32453
35423


# Program to find sum of digits of a number
echo “Enter Number”
read num
sum=0
while [ $num -ne 0 ]
do
sum=`expr $sum + $num % 10`
num=`expr $num / 10`
done
echo Sum of Digits = $sum

# Output
# Enter Number
# 234
# Sum of Digits = 9

# Enter Number
# 23
# Sum of Digits = 5


Shell Program check whether a given number is odd or even

echo "Enter Number"
read num
if [ $num -eq 0 ]
then
echo "Neither Even nor Odd"
elif [ `expr $num % 2` -eq 0 ]
then
echo "Number is Even"
else
echo "Number is Odd"
fi

#Output

# Enter Number
# 2
# Number is Even

# Enter Number
# 3
# Number is Odd

# Enter Number
# 0
# Neither Even nor Odd

No comments:

Post a Comment