Вы находитесь на странице: 1из 12

1. write a shell script to print the following pttrns for any no.

of lines supplied as command line


argument
*
**
***

Source:
echo "-------------------------"
echo " Pattern"
echo "-------------------------"
echo
for(( i =1 ; i<=$1 ; i++ )) do
for(( j=0 ; j<i ; j++ )) do
echo -n "*"
done
echo
done
Output:

2. write a shell script to print all non prime nos. within a range where range is taken as input
from the user.

Source:
echo '--------------------------------'
echo ' Non-Prime printing '
echo '--------------------------------'
echo "Enter upper range:"
read up
echo "Enter lower range"
read low
echo 'non-Primes are: '
for((i = low; i <= up;i++)) do
flag=0
for((j = 2;j < i;j++)) do
r=`expr $i % $j`
if [ $r -eq 0 ]; then
flag=1
fi
done
if [ $flag -ne 0 ]; then
echo $i
fi
done
Output:
3.write a shell script to calculate the grade of the student from the avg. marks where the marks of
5 subjects are given by the user. determine the grade as per the following rules :
>= 70%.. . A
60-70%.. B
50-60%. C
40-50%.. D
<= 40%.. F

Source:
echo -----------------------------------
echo ' Student Mark List'
echo -----------------------------------
echo Enter the Student name
read name
echo Enter the Register number
read rno
echo Enter the Mark1
read m1
echo Enter the Mark2
read m2
echo Enter the Mark3
read m3
echo Enter the Mark4
read m4
echo Enter the Mark5
read m5
tot=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
avg=$(expr $tot / 5)
echo -----------------------------------
echo 'Student Mark List'
echo -----------------------------------
echo "Student Name : $name"
echo "Register Number : $rno"
echo "Mark1 : $m1"
echo "Mark2 : $m2"
echo "Mark3 : $m3"
echo "Mark4 : $m4"
echo "Mark5 : $m5"
echo "Total : $tot"
echo "Average : $avg"
if [ $m1 -ge 35 ] && [ $m2 -ge 35 ] && [ $m3 -ge 35 ] && [ $m4 -ge 35 ] && [ $m5 -ge 35 ]
then
echo "Result : Pass"

if [ $avg -ge 80 ]
then
echo "Grade : A"
elif [ $avg -ge 70 ]
then
echo "Grade : B"
elif [ $avg -ge 60 ]
then
echo "Grade : C"
elif [ $avg -ge 50 ]
then
echo "Grade : D"
elif [ $avg -ge 40 ]
then
echo "Grade : D"
fi
else
echo "Result : Fail"
fi
echo -----------------------------------

Output:

4. write a shell script to print the reverse of a string where string is supplied as command line
argument

Source:
echo "--------------------------"
echo " String Reverse"
echo "--------------------------"
read -p "Enter string:" string
len=${#string}
echo "Reverse:"
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"

Output:

5. write a shell script to show the max of N nos. where N is taken from user as input.

Source:
echo "Enter Size(N)"
read N
i=1
max=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num
if [ $i -eq 1 ] #set first number as max
then
max=$num
else
if [ $num -gt $max ]
then
max=$num
fi
fi
i=$((i + 1)) #increment i by 1
done

echo "Largest Number is: " $max


Output:

6. write a shell script that will perform the bubble sort on a set of N elements taken as input

Source:

echo "========================="
echo " Bubble Sort"
echo "========================="
echo "enter the number of input"
read n
# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
#printing the number before sorting
echo "Numbers in an array are:"
for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number
echo -e "\nSorted Numbers "
for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
Output:

7. write a shell script to evaluate the following series where N is taken as input from the user : S=
1+2.2+3.3+....+n.n

Source:
echo "----------------------"
echo " Sum of Series"
echo "----------------------"
echo "Enter Size(N)"
read N

i=1
sum=0
echo "Series: 1+2.2+....$N.$N"
while [ $i -le $N ]
do
sum=$((sum + i*i)) #sum+=num
i=$((i + 1))
done

echo $sum
Output:

8. write a shell script to find the position of the sub string in the main string where main string is
taken as input from the user

Source:
echo "-----------------"
echo " String Position"
echo "-----------------"
echo Enter main string:
read main
l1=`echo $main | wc -c`
l1=`expr $l1 - 1`
echo Enter sub string:
read sub
l2=`echo $sub | wc -c`
l2=`expr $l2 - 1`
n=1
m=1
pos=0
while [ $n -le $l1 ]
do
a=`echo $main | cut -c $n`
b=`echo $sub | cut -c $m`
if [ "$a" = "$b" ]
then
n=`expr $n + 1`
m=`expr $m + 1`
pos=`expr $n - $l2`
r=`expr $m - 1`
if [ "$r" -eq "$l2" ]
then
break
fi
else
pos=0
m=1
n=`expr $n + 1`
fi
done
echo Position of sub stringin main stringis $pos
Output:

9. write a shell script to perform the arithmetical operation


(addition,substraction,multiplication,division,etc) of the basic calculator according to the user
choice.

Source:
echo -----------------------------------------------------
echo ' Evaluation of Arithmetic expression'
echo -----------------------------------------------------
echo Enter the a value
read a
echo Enter the b value
read b
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Modules
echo Enter your choice
read choice
case $choice in
1)echo Addition : $(expr $a + $b);;
2)echo Suubtraction : $(expr $a - $b);;
3)echo Multiplication : $(expr $a \* $b);;
4)echo Division : $(expr $a / $b);;
5)echo Modules : $(expr $a % $b);;
*)echo This is not a choice
esac
Output:

10. write a shell script to take as input the user choice and do the following operation as power
the user choice :
...... 1. to count the no. of lines,word,character in a file
...... 2. to reverse the content of the file.
Source:
clear
echo "--------------------"
echo " File Handling"
echo "--------------------"
echo "\n"
echo "Enter the filename"
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo "1) Count character, line, word"
echo "2)Reverse"
echo Enter Choice:
read choice
case $choice in
1)echo Count: Number of characters: $c , number of words: $w , number of lines: $l in
$file;;
2)echo "Reverse contents :"
rev $file
esac

Output:

Вам также может понравиться