Unix
Unix
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:
REVERSE ORDER
--------------------------
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:
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
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
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:
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:
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
CODING:
CODING
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
OUTPUT:
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]$
OUTPUT:
[mca @ localhost mca]$ sh eligible.sh
Enter the user’s age:
25
You are allowed to drive any vehicle
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
*****************************
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:
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:
ELECTRICITY BILL
CUSID NAME ADDRESS PRE_READING CURREADING UNITS
AMOUNT