You are on page 1of 39

FIBONACCI SERIES

CODING:
echo “Enter number of times ”
read number
fib1=0
fib2=1
echo $fib1
echo $fib2
((number=$number-2))
while(($number!=”0”))
do
((fib=$fib1+$fib2))
((fib1=$fib2))
((fib2=$fib))
((number=$number-1))
echo $fib
done
OUTPUT:
[mca @ localhost mca]$ sh fibo.sh
Enter number of times
8
0
1
1
2
3
5
8
13
[mca @ localhost mca]$
FINDING GREATEST VALUE AMONG THREE
NUMBERS

CODING:
echo “Enter a value”
read a
echo “ Enter b value”
read b
echo “ Enter c value”
read c
if test $a –gt $b –a $a –gt $c
then
echo “a is greatest”
else
if test $b –gt $c
then
echo “b is greatest”
else
echo “c is greatest”
fi
fi
OUTPUT:
[mca @ localhost mca]$ sh greatest.sh
Enter a value
7
Enter b value
8
Enter c value
6
b is greatest
[mca @ localhost mca]$
TO KNOW THE USER IS LOGGED IN OR NOT

CODING:
echo “Enter the user id”
read uid
if who|grep $uid >/dev/null
then
echo $uid “has logged in”
else
echo $uid “not logged in”
fi
OUTPUT:
[mca @ localhost mca]$ sh userid.sh
Enter the user id
mca
mca has logged in
[mca @ localhost mca]$ sh userid.sh
Enter the user id
mca1
not logged in
[mca @ localhost mca]$
FINDING ARMSTRONG NUMBER
CODING:
echo “ Enter a number”
read n
i=n
k=0
j=0
while(($i>0))
do
((k=i%10))
((j=j+(k*k*k)))
((i=i/10))
done
if((n==j))
then
echo $a “is an Armstrong number”
else
echo $a “is not an Armstrong number”
fi
OUTPUT:
[mca @ localhost mca]$ sh arms.sh
Enter a number
153
153 is an Armstrong number
[mca @ localhost mca]$ sh arms.sh
Enter a number
335
335 is not an Armstrong number
TO RENAME REMOVE COPY AND EXIT A FILE
CODING:
echo “Main menu “
echo “1.Rename”
echo “2.Remove”
echo “3.Copy”
echo “4.Exit”
echo “Enter ur choice”
read ch
if test $ch -eq “1”
then
echo “Enter the old filename:”
read filename1
echo “Enter the new filename:”
read filename2
mv $filename1 $filename2
echo $filename1 “the file has been renamed as” $filename2
elif test $ch –eq “2”
then
echo “Enter the filename to remove”
read filename3
rm $filename3
echo $filename3 “the file has been removed”
elif test $ch –eq “3”
then
echo “Enter the old filename:”
read filename4
echo “Enter the new filename:”
read filename5
cp $filename4 $filename5
echo $filename4 “the file has been copied to” $filename5
elif test $ch –eq “4”
then
echo “Quit”
fi
OUTPUT:
[mca @ localhost mca]$ sh menus.sh
Main menu
1.Rename
2.Remove
3.Copy
4.Exit
Enter ur choice:
1
Enter the old filename:
file1.sh
Enter the new filename:
nfile.sh
file1.sh the file has been renamed as nfile.sh
Enter ur choice:
2
Enter the filename to remove
File1.sh
File1.sh the file has been removed
Enter ur choice:
3
Enter the old filename:
File3.sh
Enter the new filename:
File4.sh
File3.sh the file has been copied to file4.sh
Enter ur choice:
4
Quit.
TO DISPLAY POSITIONAL PARAMETERSAND
REVERSE ORDER

CODING:

echo “PROGRAM TO DISPLAY THE POSITIONAL PARAMETERS”


echo “********************************************”
echo
set i am working as a lecturer
echo $0
echo $3 $4 $5 $6
echo $1 $2 $3 $4 $10 $11 $12
echo $20 $21 $22 $23
shift 3
echo $1 $2 $3 $4

echo “ REVERSE ORDER “


echo “ ------------------------- “
echo
set I am, I am studying first year mca student of mscas.
Echo $9 $8 $7 $6 $5 $4 $3 $2 $1
Shift 9
echo $1 $2
OUTPUT:

[mca @ localhost mca]$ shreverserder.sh

PROGRAM TO DISPLAY THE POSITIONAL PARAMETERS


********************************************
Reverse.sh
Working as a lecturer
I am working as i0 i1 i2
Am0 am1 am2 am3
As a lecturer

REVERSE ORDER
--------------------------

Mca year first studying am I ,am i


Student of
SUM OF SQUARE OF THE FIRST N EVEN NUMBERS

CODING:
echo
echo
echo “ SUM OF SQUARE OF THE FIRST n EVEN NUMBERS”
echo “ ========================================”
echo
echo
echo “Enter n value”
read n
echo
j=1
i=0
sum=0
while(($j<=$n))
do
((sqr=$i*$i))
((i=$i+2))
((sum=$sum+$sqr))
echo I SQUARE OF I
echo $i $sqr
((j=j+1))
Done
echo “sum of square of first “$n”even numbers=” $sum
OUTPUT:

[mca @ localhost mca]$ sh square.sh


SUM OF SQUARE OF THE FIRST N EVEN NUMBERS
=========================================

Enter n value
10

I SQUARE OF I

0 0
2 4
4 16
6 36
8 64
10 100
12 144
14 196
16 256
18 324

Sum of square of the first 10 even numbers = 1140


GENERATING MULTIPLICATION TABLE FOR ANY
NUMBER

CODING:
echo
echo
echo” MULTIPLICATION TABLE GENERATING FOR ANY NUMBER”
echo
echo
i=1
echo “Enter the number”
read num
while (($i<=”20”))
do
((multi=$i*$num))
echo “ “$number “ * ”$i “ =” $multi
((i=$i+1))
done
echo “End of the program”
OUTPUT:
[mca @ localhost mca]$ sh multi.sh
MULTIPLICATION TABLE GENERATIN FOR ANY NUMBER

Enter the number 5


5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70
5 * 15 = 75
5 * 16 = 80
5 * 17 = 85
5 * 18 = 90
5 * 19 = 95
5 * 20 = 100

End of the program


FINDING SUM OF DIGITS OF GIVEN NUMBER

CODING:
echo
echo
echo “ SUM OF DIGITS OF A GIVEN NUMBER”
echo “ ******************************”
echo
echo
echo “Enter any number”
read num
echo “Enter number of digits “
read digits
j=$num
sum=0
while(($digits !=0))
do
((sum=$sum+$j%10))
((digits=$digits-1))
((j=j/10))
done
echo “ SUM OF DIGITS OF GIVEN NUMBER “ $sum
OUTPUT:

[mca @ localhost mca]$ sh digits.sh


SUM OF DIGITS OF A GIVEN NUMBER
******************************

Enter any number


1010

Enter number of digits


4

2
FINDING SUM OF THE FIRST ODD NUMBERS

CODING:

echo
echo
echo “ SUM OF THE FIRST n ODD NUMBERS”
echo “ =============================”
echo
echo
echo “Enter n value”
read n
echo
j=1
i=1
sum=0
while(($j<=$n))
do
((sum=$sum+$i))
echo $i
((i=$i+2))
((j=j+1))
done
echo “sum of the first “$n”odd numbers=” $sum
OUTPUT:
[mca @ localhost mca]$ sh odd.sh

Enter n value
10
1
3
5
7
9
Sum of the first 10 odd numbers = 25
SUM OF CUBE OF THE FIRST N EVEN NUMBERS

CODING:
echo
echo
echo “ SUM OF CUBE OF THE FIRST n EVEN NUMBERS”
echo “ ========================================”
echo
echo
echo “Enter n value”
read n
echo
j=1
i=0
sum=0
while(($j<=$n))
do
((cube=$i*$i*$i))
((i=$i+2))
((sum=$sum+$cube))
echo I CUBE OF I
echo $i $cube
((j=j+1))
Done
echo “sum of cube of the first “$n”even numbers=” $sum
OUTPUT:

[mca @ localhost mca]$ sh cube.sh


SUM OF CUBE OF THE FIRST N EVEN NUMBERS
=========================================

Enter n value
10

I CUBE OF I

0 0
2 8
4 64
6 216
8 512
10 1000
12 1728
14 2744
16 5184
18 5832

Sum of square of the first 10 even numbers = 17288


TO GIVEN NUMBER IS PRIME OR NOT

CODING:

echo “Enter the number “


read num
k=0
l=0
j=1
while(($j<=$num))
do
((l=$num%$j))
if(($l==0))
then
((k=k+1))
fi
((j=j+1))
done
if(($k>2))
then
echo $num “is not a prime number”
else
echo $num “is a prime number”
fi
OUTPUT:
[mca @ localhost mca]$sh prime.sh
Enter the number
5
5 is a prime number
[mca @ localhost mca]$ prime.sh
Enter the number
66
66 is not a prime number
[mca @ localhost mca]$
LEAP YEAR CHECKING

CODING

echo “ enter the year“


read year
echo “enter the month”
read month
leap=0
((leap=$year%4))
if(($leap==0))
then
echo $year “is a leap year”
else
echo $year “is not a leap year”
fi
if(($month==1))
then
echo “month is jan with 31 days”
elif(($month==2))
then
if(($leap==0))
then
echo “month is feb with 29 days”
else
echo “month is feb with 28 days”
fi
elif(($month==3))
then
echo “month is mar with 31 days”
elif(($month==4))
then
echo “month is apr with 30 days”
elif(($month==5))
then
echo “month is may with 31 days”
elif(($month==6))
then
echo “month is june with 30 days”
elif(($month==7))
then
echo “month is july with 31 days”
elif(($month==8))
then
echo “month is aug with 31 days”
elif(($month==9))
then
echo “month is sep with 30 days”
elif(($month==10))
then
echo “month is oct with 31 days”
elif(($month=11))
then
echo “month is nov with 30 days”
elif(($month==12))
then
echo “month is dec with 31 days”
fi

OUTPUT:
[mca @ localhost mca]$ sh leapyear.sh
Enter year
2008
Enter month
2
2008 is a leap year
The month is feb with 29 days
[mca @ localhost mca]$ sh leapyear.sh
Enter year
2009
Enter month
11
2009 is not a leap year

The month is nov with 30 days


[mca @ localhost mca]$

FINDING FILE PERMISSION


CODING:

echo “ Enter any file name”


read fname
if test –s $fname
then
echo “File exists and has a size greater than 0”
elif test –f $fname
then
echo “File exists and is not a directory”
elif test –d $fname
then
echo “File exists and is a directory file”
fi
if test –r $fname –a –x $fname –a –w $fname
then
echo “File has all permissions”
else
if test –r $fname
then
echo “file has only read permission”
el if test –w $fname
then
echo “file has only write permission”
el if test –x $fname
then
echo “file has only execute permission”
else
echo “file has no permission”
fi
fi
OUTPUT:

[mca @ localhost mca]$ sh fileper.sh


Enter any file name
File1.sh
File exists and is not a directory
File has only read permission
[mca @ localhost mca]$ sh fileper.sh
Enter any file name
File2.sh
File exists and has a size greater than 0
File has only read permission
[mca @ localhost mca]$ sh fileper.sh
Enter any file name
File3.sh
File has no permissions

SUM OF SQUARE OF THE FIRST N NATURAL


NUMBERS
CODING:
echo
echo
echo “ SUM OF SQUARE OF THE FIRST n NATURAL NUMBERS”
echo “ ========================================”
echo
echo
echo “Enter n value”
read n
echo
i=1
sum=0
while(($I<=$n))
do
((sqr=$i*$i))
((i=$i+1))
((sum=$sum+$sqr))
echo I SQUARE OF I
echo $i $sqr
Done
echo “sum of square of first “$n” natural numbers=” $sum

OUTPUT:

[mca @ localhost mca]$ sh natural.sh


SUM OF SQUARE OF THE FIRST n NATURAL NUMBERS
=========================================

Enter n value
10

I SQUARE OF I

1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Sum of square of the first 10 natural numbers = 385
[mca @ localhost mca]$

CHECKING THE ELIGIBILITY TO DRIVE THE


VEHICLE
CODING:

echo “Enter the user’s age”


read age
if test $age –ge “18”
then
echo “you are allowed to drive any vehicle”
else
if test $age –lt “18”
then
year=18-$age
echo “you want to wait to drive any vehicle for” $year “years”
else
echo “Invalid age”
fi
fi

OUTPUT:
[mca @ localhost mca]$ sh eligible.sh
Enter the user’s age:
25
You are allowed to drive any vehicle

[mca @ localhost mca]$ sh eligible.sh

Enter the user’s age:


16
You want to wait to drive any vehicle for 2 years

PRINTING MARKLIST OF THE STUDENTS


CODING:

BEGIN{
print “\n\n MARKLIST OF FIRST MCA NOV-2010 \n\n”
print “**********************************”
print “\n Reg.no \t Name \t M1 \t M2 \t M3 \t M4 \t Total \t Per \t Grade \n\n”}
{((Total=$3+$4+$5+$6))
((Percentage=Total/4))
if(Percentage >= 75)
{((Grade=” Outstanding”))}
else
if(Percentage >=60)
{((Grade=”First class))}
else
if(Percentage >=50)
{((Grade=”Second class”))}
else
{((Grade=”Fail”))}
print $1,”\t”,$2,”\t”,$3,”\t”,$4,”\t”,$5,”\t”,$6,”\t”,Total,”\t”,Percentage,”\t”,Grade
}
END {print “The number of records processed”NR”.”
print “The name of the file is “FILENAME}

OUTPUT:
[mca @ localhost mca]$ awk –f student.awk marklist
MARKLIST OF FIRST MCA NOV-2010
*****************************

Reg.no Name M1 M2 M3 M4 Total Per Grade

1001 Geetha 90 90 90 90 360 90 Outstanding


1005 Gandhi 60 60 60 64 244 61 First class
1007 saravannan 50 50 50 54 204 51 Second class
1010 Arun 40 46 44 42 172 43 Fail

The number of records processed 4.


The name of the file is marklist.
[mca @ localhost mca]$

PRINTING THE SALARY REPORT OF A EMPLOYEE

CODING:
BEGIN{
print “\n\t\t SALARY REPORT\N\N”
print “\nS.NO \t NAME \t DESIGNATION \t BASIC PAY \t DA \t HRA \t GROSS PAY\n”
print “************************************************************\n\n”
}
{
sno++
if($3>10000)
{
da= 0.12*$3
hra=0.15*$3
}
else
{
da=0.12*$3
hra=0.20*$3
}
totsal=$3+da+hra
print “%2d %10s %10s %8.2f %8.2f %8.2f %8.2f\n”,sno,$1,$2,$3,$3+$da+$hra
}
Total+=$3+$da+$hra
END {print “\n\t\t Total salary paid is Rs.”,total}

OUTPUT:

[mca @ localhost mca]$ awk –f salary.awk sal


SALARY REPORT

S.NO NAME DESIGNATION BASIC PAY DA HRA


GROSS PAY
***************************************************************************

1 DHIVYA S/W MANAGER 15000.00 1800.00 2250.00 19050.00


2 RAM DIRECTOR 20000.00 2400.00 3000.00 25400.00

Total salary paid is Rs.44450

[mca @ localhost mca]$

GENERATE ELECTRICITY BILL

CODING:
BEGIN{
print “\n\t\t ELECTRICITY BILL \n”
print “ \t\t*****************\n\n”
print “\n\n CUSID \t NAME \t ADDRESS \t PRE_READING \t CUR_READING \t UNITS \t
AMOUNT\n”
}
{((units=$5-$4))
if(units<=50)
{((amt=units*1.50))}
else
if(units<=100)
{((amt=units*1.75))}
else
if(units<=200)
{((amt=units*2.00))}
else
{((amt=units*5))}
print $1,”\t”,$2,”\t”,$3,”\t”,$4,”\t”,$5,”\t”,$6,”\t”,units,”\t”,amt
}
END { print “\n\n the number of records processed “NR
Print “\n\n the name of the input file is “FILENAME}

OUTPUT:

[mca @ localhost mca]$ awk –f electric.awk ebill

ELECTRICITY BILL
CUSID NAME ADDRESS PRE_READING CURREADING UNITS
AMOUNT

1111 HARI MSCAS 250 600 350 1750


1112 SINDHU MSCAS 250 350 100 150

The number of records processed :2

The name of the input file is ebill.

You might also like