Saturday, January 11, 2014

MCA UNIX Programming Practical University of Madras

/* 1.LICENCE */
CODING:
i=18
echo enter your age
read n
echo your age is $n
if [ $n -gt 18 ]
then
echo you are allowed to drive the car
fi
if [ $n -lt 16 ]
then
echo you are not allowed to drive the car
w=`expr $i - $n`
echo you have to wait for $w years
fi









OUTPUT:
user@ubuntu:~$ sh mk.sh
enter your age
20
your age is 20
you are allowed to drive the car
user@ubuntu:~$ sh mk.sh
enter your age
12
your age is 12
you are not allowed to drive the car
you have to wait for 6 years










/* 2.LEAP YEAR */
CODING:
echo enter the month
read m
echo enter the year
read y
if [ `expr $y % 4` -eq 0 -a `expr $y % 100` -ne 0 -o `expr $y % 100` -eq 0 -a `expr $y % 400` -eq 0 ]
then
echo $y is a leap year
if [ $m -eq 2 ]
then
echo No.of days in this month is 29
fi
else
echo $y is not a leap year
if [ $m -eq 2 ]
then
echo no of days in this month is 28
fi
fi
if [ $m -eq 1 -o $m -eq 3 -o $m -eq 5 -o $m -eq 7 -o $m -eq 8 -o $m -eq 10 -o $m -eq 12 ]
then
echo $y is a leap year
if [ $m -eq 2 ]
then
echo No.of days in this month is 29
fi
else
echo $y is not a leap year
if [ $m -eq 2 ]
then
echo no of days in this month is 28
fi
fi
if [ $m -eq 1 -o $m -eq 3 -o $m -eq 5 -o $m -eq 7 -o $m -eq 8 -o $m -eq 10 -o $m -eq 12 ]
then
echo no of days in this month is 31
fi
if [ $m -eq 4 -o $m -eq 6 -o $m -eq 9 -o $m -eq 11 ]
then
echo no of days in this month is 30
fi





OUTPUT:
user@ubuntu:~$ sh leap.sh
enter the month:2
enter the year:2000
2000 is a leap year
enter the month:6
enter the year:2012
2012 is a leap year
no of days in this month is 30



































3.PRIME/ARMSTRONG/PERFECT

CODING:
echo enter a number
read n
i=2
flag=1
while [ $i -lt $n ]
do
c=`expr $n % $i`
if [ $c -eq 0 ]
then
flag=0
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 1 ]
then
echo $n is prime
else
echo $n is not prime
fi



OUTPUT:
user@ubuntu:~$ sh pri.sh
enter a number:23
23 is prime
enter a number: 18
18 is not prime













/*B.ARMSTRONG NUMBER */

CODING:
echo enter the number
sum=0
read n
n1=$n
while [ $n1 -gt 0 ]
do
s=`expr $n1 % 10`
n1=`expr $n1 / 10`
sum=`expr $sum + $s \* $s \* $s`
done
if [ $sum -eq $n ]
then
echo $n is a armstrong no
else
echo $n is not a armstrong no
fi





OUTPUT:
user@ubuntu:~$ sh arm1.sh
enter the number
153
153 is a armstrong no
user@ubuntu:~$ sh arm1.sh
enter the number
154
154 is not a armstrong no













/*C.Perfect Number*/
CODING:
echo Enter the number
read n
dn=`expr $n \* 2`
i=1
sum=0
echo The divisors are
while [ $i -le $n ]
do
if [ `expr $n % $i` -eq 0 ]
then
sum=`expr $sum + $i`
echo $i
fi
i=`expr $i + 1`
done
echo sum = $sum
echo dn = $dn
if [ $sum -eq $dn ]
then
echo $n is  a perfect no
else
echo $n is not a perfect no
fi


OUTPUT:
user@ubuntu:~$ sh perfect.sh
Enter the number
28
The divisors are
1
2
4
7
14
28
sum = 56
dn = 56
28 is a perfect no





/*4.REVERSE ORDER*/
CODING:
x=" "
echo -n "numbers are :"
for n in $@
do
echo -n $n
echo -n " "
x="$n $x"
done
echo " "
echo -n "reverse order :"
echo $x



OUTPUT:
user@ubuntu:~$ sh revers.sh 1 2 3 4 5 6 7 8 9
numbers are :1 2 3 4 5 6 7 8 9
reverse order :9 8 7 6 5 4 3 2 1


























/*5.HANDLING ARGUMENTS*/
CODING:
echo First arg is :$1
echo third argument :$3
echo tenth argument :${10}
echo total ags $#
echo the arguments are
for n in $@
do
echo -n $n
echo -n " "
done
shift
shift
shift
echo
echo the remaing no of args $#
for n in $@
do
echo -n $n





OUTPUT:
user@ubuntu:~$ sh parame.sh 1 2 3 4 5 6  7 8 9 10
First argument  is :1
third argument  is :3
tenth argument  is :10
total arguments: 10
the arguments are:
1 2 3 4 5 6 7 8 9 10
the remaining no of arguments are:
7 4 5 6 7 8 9 10











/*6.COPY REMOVE MOVE FILE*/  CODING:
Clear
echo "menu"
echo "1.copy a file"
echo "2.remove a file"
echo "3.move a file"
echo "4.quit"
echo "Enter your choice \c"
read choice
case "$choice" in
1)echo "Enter file name to copy \n"
read f1
echo "Enter file name \c"
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2)echo "Enter the file to be removed"
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist"
fi
;;
3)echo "Enter the file name to move \c"
read f1
echo "Enter destination \c"
read f2
if [ -f $f1 ]
then
if [ $f2 ]
then
mv $f1 $f2
fi
echo "$f1 does not exist"
fi
;;
4)echo "exit"
exit ;;
esac



OUTPUT:
menu
1.copy a file
2.remove a file
3.move a file
4.quit
Enter your choice 1
Enter file name to copy

one.sh
Enter file name two.sh
































/*7.LOGIN OR NOT*/
CODING:
read u
if [ `who | grep -c $u` -eq 0 ]
then
echo "$u IS NOT LOGGED INTO THE SERVER !!!"
echo
exit 0
else
echo
echo "$u IS LOGGED INTO THE SERVER !!!"
echo
fi




OUTPUT:
user@ubuntu:~$ sh login.sh
u

u IS LOGGED INTO THE SERVER !!!
t

t IS LOGGED INTO THE SERVER !!!























/* 8.FILE PERMISSION */
CODING:
echo -n "Enter File Name :"
read file
[ -w $file ] && W="Write = Yes" || W="Write = No"
[ -x $file ] && X="Execute = Yes" || X="Execute = No"
[ -r $file ] && R="Read = Yes" || R="Read = No"
[ -d $file ] && D="Directory" || D="Directory = No"
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
echo "$D"







OUTPUT:
user@ubuntu:~$ sh permission.sh
Enter File Name :per.sh
per.sh permissions
Write = Yes
Read = Yes
Execute = No
Directory = No
















/*9. NEWS UPDATE*/
CODING:
echo enter 1 for the all news and 2 for new news
read n
if [ $n -eq 1 ]
then
echo this will display all news new and old news -a | more
fi
if [ $n -eq 2 ]
then
echo this will display only new news
news | more
fi












OUTPUT:
user@ubuntu:~$ sh news.sh
enter 1 for the all news and 2 for new news
1
this will display all news new and old news –a




















/*10.  DOWNLOAD OR UPLOAD THE FILE*/
CODING:
echo enter the file name
read fn
echo enter 1 for upload 2 for download
read n
if [ $n -eq 1 ]
then
echo file is beeing uploaded
curl -K -F myfile=@one.sh -H `HTTP_AUTH_LOGIN:admin` -H `HTTP_AUTH_PASSWD:password` http://google.com
fi
if [ $n -eq 2 ]
then
echo file is beeing downloaded
curl $fn
fi








OUTPUT:
user@ubuntu:~$ sh download.sh
enter the file name
one.sh
enter 1 for upload 2 for download
1
file is beeing uploaded













/*11. CAT COMMAND*/
CODING:
echo enter the file name to display
read fn
echo enter the column no
echo another way to print the file content is
if [ $n -eq 1 ]
then
cat $fn | awk '{print $1}'
fi
if [ $n -eq 2 ]
then
cat $fn | awk '{print $2}'
fi
if [ $n -eq 3 ]
then
cat $fn | awk '{print $3}'
fi
OUTPUT:
user@ubuntu:~$ sh filedisplay.sh
enter the file name to display
one1.c
enter the column no
1
another way to print the file content is
#include
void
{
printf
}
user@ubuntu:~$ sh filedisplay.sh
enter the file name to display
one1.c
enter the column no
2
another way to print the file content is
<stdio.h>
main()
("hello");




/*12. SIMULATE FIND COMMAND*/
CODING:
read fname
i=`ls | grep -c $fname`
echo $i
if [ $i -eq 1 ]
then
echo
echo "$fname found!!!"
echo
exit 0
else
echo
echo "$fname not found!!!"
echo
fi









OUTPUT:
user@ubuntu:~$ sh find.sh
one.c
1
one.c found!!!
user@ubuntu:~$ sh find.sh
one.sh
0
one.sh not found!!!












/*13.REMOVE DUPLICATE WORDS */
CODING:
echo removing duplicate words
echo enter the file name
read fn
sort -u $fn



OUTPUT:
user@ubuntu:~$ sh thirteen.sh
removing duplicate words
enter the file name
two.txt
hello
world






























/* 14. Electricity  Billing System */
awk '{print $1"\t" $2"\t" $3 "\t"$4"\t" $5"\t" $6}' ebhead.dat
awk '
     {
       { u = $3 - $4 }
       { a = u * 3 }
       {print $1"\t"$2"\t"$3"\t"$4"\t"u"\t"a"\t"}
       {c = 0;}

}' eb.dat

eb.dat
1000 sun 8787 8666
1001 sen 9999 9888

behead.dat
Number Name C-R P-R Unit RS 
Output
Number Name C-R   P-R     Unit   RS 
1000       sun    8787  8666    121    363
1001       sen    9999   9888    111    333

























/*15. Student Mark List */
awk '{print $1"\t" $2"\t" $3 "\t"$4"\t" $5"\t" $6"\t" $7"\t" $8"\t" $9}' head.dat
awk '
     {
      for(i = 2; i<= NF; i++)
       { c = c + $i }
       { a = c / 5}
       {print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"c"\t"a"\t"}
       {c = 0;}

}' st.dat



st.dat
1000 sun 50 50 50 50 50
1001 sen 60 60 60 60 60
head.dat
Name No M1 m2 m3 m4 m5  total Avg

Output
Name No M1 m2 m3 m4 m5  total Avg
1000 sun  50  50  50   50  50    250   50
1001 sen 60   60  60   60  60     300  50



No comments: