B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
Shell Programming
1. Write a Shell program to check the given number is even or odd.
PROGRAM
#shell script to check the given number is even or odd.
echo “Enter a number:”
read n
if [ `expr $n % 2` = 0 ]
then echo “Even number”
else echo “Odd number”
fi
2. Write a Shell program to find the area and circumference of a circle.
PROGRAM
#shell script to find the area and circumference of a circle.
echo “Enter the radius:”
read r
area=`echo 3.14 \* $r \* $r | bc`
cir=`echo 2 \* 3.14 \* $r | bc`
echo “Area : $area”
echo “Circumference : $cir”
3. Write a Shell program to check the given number and its reverse are same.
PROGRAM
#shell script to check the given number and its reverse are same.
echo “Enter a number:”
read n
t=$n
s=0
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $r + $s \* 10`
n=`expr $n / 10`
done
if [ $s = $t ]
then echo “The given number and its reverse are same”
else echo “The given number and its reverse are not same”
fi
4. Write a Shell program to check the given string is palindrome or not.
PROGRAM
#shell script to check the given string is palindrome.
echo "Enter a string:"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
5. Write a Shell program to check the given integer is Armstrong number or not.
PROGRAM
#shell script to check the given number is Armstrong.
echo “Enter a number:”
read n
t=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r \* $r \* $r`
n=`expr $n / 10`
done
if [ $s = $t ]
then echo “$t is an armstrong number”
else echo “$t is not an armstrong number”
fi
6. Write a Shell program to check the given integer is prime or not.
PROGRAM
#shell script to check the given number is prime.
echo "Enter the number"
read a
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
i=2
z=0
while [ $i -lt $a ]
do
s=`expr $a % $i`
if [ $s -eq $z ]
then
echo "Not Prime"
exit
else
i=`expr $i + 1`
fi
done
echo "Prime number"
7. Write a Shell program to find the sum of squares of individual digits of a number.
PROGRAM
#shell script to find the sum of squares of individual digits of a number.
echo “Enter a number:”
read n
t=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r \* $r`
n=`expr $n / 10`
done
echo “The sum of square of individual digits of $t is $s”
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
8. Write a Shell program to execute various LINUX commands using case statement.
PROGRAM
#shell script to execute various Linux commands using case-esac.
echo “1-who am I?”
echo “2-who is logged on?”
echo “3-date”
echo “4-calendar”
echo “Enter your choice:”
read n
case $n in
1)whoami ;;
2)who ;;
3) date ;;
4) cal ;;
esac
9. Write a Shell program to count the number of vowels in a line of text.
PROGRAM
#shell script to count the number of vowels
clear
echo "Entre a string to find the number of Vowels "
read st
len=`expr $st | wc -c`
len=`expr $len - 1`
count=0
while [ $len -gt 0 ]
do
ch=`expr $st | cut -c $len`
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
case $ch in
[aeiou,AEIOU]) count=`expr $count + 1` ;;
esac
len=`expr $len - 1`
done
echo "Number of vowels in the give string is $count"
10. Write a Shell program to display student grades.
PROGRAM
#shell script to find grade.
echo "Enter the marks of five subjects for the student"
read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo "Total Marks: " $sum1
per=`expr $sum1 / 5`
echo " Percentage: " $per
if [ $per -ge 80 ]
then echo "Grade: Distinction"
elif [ $per -ge 60 ]
then echo “Grade: First Class”
elif [ $per -ge 40 ]
then echo "Grade: Second Class"
else echo "Grade: Failed"
fi
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
11. Write a Shell program to find the largest number from N numbers.
PROGRAM
#shell script to find largest of n numbers
echo "Enter Size(N)"
read N
i=1
max=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num
if [ $i -eq 1 ]
then max=$num
else if [ $num -gt $max ]
then max=$num
fi
fi
i=$((i + 1))
done
echo "Largest number is: $max"
12. Write a Shell program to find the smallest digit from a number.
PROGRAM
#shell script to find the smallest digit from a number.
echo “Enter a number:”
read n
s=9
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
while [ $n -gt 0 ]
do
r=`expr $n % 10`
if [ $r -lt $s ]
then s=$r
fi
n=`expr $n / 10`
done
echo “The smallest digit is : $s”
13. Write a Shell program to find the sum of digits of a number until a single digit is obtained.
PROGRAM
#shell script to find the sum of digits of a number until a single digit is obtained.
echo “Enter a number:”
read n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r`
n=`expr $n / 10`
if [ $n = 0 -a $s -gt 9 ]
then n=$s
s=0
fi
done
echo “The single digit sum is : $s”
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
14. Write a Shell program to find the second largest digit from a number.
PROGRAM
#shell script to find the second largest digit from a number.
echo “Enter a number:”
read n
a=0
b=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
if [ $r -gt $a ]
then b=$a
a=$r
elif [ $r -gt $b ]
then b=$r
fi
n=`expr $n / 10`
done
echo “The second largest digit is : $b”
15. Write a Shell program to find the sum of odd digits and even digits from a number.
PROGRAM
#shell script to find the sum of odd and even digits of a number.
echo “Enter a number:”
read n
os=0
es=0
while [ $n -gt 0 ]
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
do
r=`expr $n % 10`
if [ `expr $r % 2` = 0 ]
then es=`expr $es + $r`
else os=`expr $os + $r`
fi
n=`expr $n / 10`
done
echo "Sum of odd digits: $os"
echo "Sum of even digits: $es"
16. Write a Shell program to find the sum of two numbers using command line argument.
PROGRAM
#shell script to find sum using command line arguments.
echo "Sum is " `expr $1 + $2`
17. Write a Shell program to find the largest of two numbers using command line arguments.
PROGRAM
#shell script to find largest of two numbers using command line arguments.
if [ $1 -gt $2 ]
then
echo “Largest number is $1”
else
echo "Largest number is $2"
fi
IV_BCA_A Linux Programming
B
BVM Holy Cross College Cherpunkal Dept. of Computer Science
18. Write a Shell program to print the reverse of a number.
PROGRAM
#shell script to reverse a number
echo “Enter a number:”
read n
t=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $r + $s \* 10`
n=`expr $n / 10`
done
echo “The reverse of the number $t is $s”
19. Write a Shell program to find the factorial of a number.
PROGRAM
#shell script for factorial of a number
echo "Enter a number"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num))
num=$((num - 1))
done
echo $fact
IV_BCA_A Linux Programming
Mar Augusthinose College, Ramapuram Dept. of Computer Science
20. Write a Shell program to generate Fibonacci series.
PROGRAM
# shell script to generate Fibonacci series
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
21. Write a shell program to sort numbers in a file in ascending order.
PROGRAM
#Program to sort numbers in ascending order from a file.
file=""
echo -n "Enter Filename : "
read file
if [ ! -f $file ]
then
IV_BCA_A Linux Programming
Mar Augusthinose College, Ramapuram Dept. of Computer Science
echo "$file not a file!"
exit 1
fi
sort -n $file
22. Write a shell program to find a specific pattern of string in a file using command line
arguments.
PROGRAM
#Program to find a pattern in a file.
PATTERN=$1
FILE=$2
if grep -i $PATTERN $FILE;
then
echo "Here are the Strings with the Pattern '$PATTERN':"
echo "$(grep -i $PATTERN $FILE)\n"
echo "The word '$PATTERN' is found $(grep -io $PATTERN $FILE | wc -l) times"
else
echo "Error: The Pattern '$PATTERN' was NOT Found in '$FILE'"
echo "Exiting..."
exit 0
fi
23. Write a shell program to find the sum and average of numbers using command line
arguments.
PROGRAM
#Program to find the sum and average of numbers using command line.
sum=0
for i in $*
do
IV_BCA_A Linux Programming
Mar Augusthinose College, Ramapuram Dept. of Computer Science
sum=`expr $sum + $i`
done
echo "Sum is: $sum"
avg=`expr $sum / $#`
echo "Average is: $avg"
24. Write a shell program to make a menu driven calculator using case.
PROGRAM
#Program to make menu driven calculator.
clear
sum=0
i="y"
while [ $i = "y" ]
do
echo "Enter first no."
read n1
echo "Enter second no."
read n2
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1) sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 - $n2`
IV_BCA_A Linux Programming
Mar Augusthinose College, Ramapuram Dept. of Computer Science
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
25. Write a shell program to print the file access permissions of a given file.
PROGRAM
#Program to print file access permissions of a file.
echo -n "Enter file name : "
read file
if [ ! -f $file ]
then
echo "$file not a file!"
echo "Exiting. .. "
exit 1
fi
if [ -w $file ]
then
IV_BCA_A Linux Programming
Mar Augusthinose College, Ramapuram Dept. of Computer Science
W="Write = yes"
else
W="Write = No"
fi
if [ -x $file ]
then
X="Execute = yes"
else
X="Execute = No"
fi
if [ -r $file ]
then
R="Read = yes"
else
R="Read = No"
fi
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
IV_BCA_A Linux Programming