Sunday, March 13, 2011

Unix Programs (Operating System Assignment)


UNIX Practicals
 



1. WAS: to display 1 to 10 numbers (while loop)

a=1
while [ $a -le 10 ]
do
echo "$a"
a=`expr $a + 1`
done

2. WAS: to display 1 to 10 numbers (for loop)

for (( a=1;a<=10;a++ ))
do
echo "$a"
done
  
3. WAS: to display 1 to 10 numbers (until loop)

a=1
until [ $a -gt 10 ]
do
echo $a
a=`expr $a + 1`
done




4. WAS: to display 10 to 1 numbers (while loop)

a=10
while [ $a -ge 1 ]
do
echo $a
a=`expr $a - 1`
done  

5. WAS: to display 10 to 1 numbers (for loop)

for (( a=10;a>=1;a-- ))
do
echo "$a"
done

6. WAS: to display 10 to 1 numbers (until loop)

a=10
until [ $a -lt 1 ]
do
echo $a
a=`expr $a - 1`
done   

7. WAS: to display 1 to N numbers (while loop)

echo "Upto what numbers you want"
read n
a=1
while [ $a -le $n ]
do
echo $a
a=`expr $a + 1`
done        




8. WAS: to display 1 to N numbers (for loop)

echo "Upto what numbers you want"
read n
for (( a=1;a<=n;a++ ))
do
echo “$a”
done

9. WAS: to display 1 to N numbers (until loop)

echo "Upto what numbers you want"
read n
a=1
until [ $a -ge $n ]
do
echo $a
a=`expr $a + 1`
done        

10. WAS: to display N to 1 numbers (while loop)

echo "enter the no"
read no
while [ $no -ge 1 ]
do
echo $no
no=`expr $no - 1`
done 

11. WAS: to display N to 1 numbers (for loop)

echo "from what numbers you want"
read n
for (( a=n;a>=1;a-- ))
do
echo “$a”
done




12. WAS: to display N to 1 numbers (Until loop)

echo "enter the no"
read no
until [ $no -le 1 ]
do
echo $no
no=`expr $no - 1`
done 

13. WAS: to find the sum of 1 to 10 numbers (while loop)

$ cat >f4
a=1
sum=0
while [ $a -le 10 ]
do
sum=`expr $sum + $a`
a=`expr $a + 1`
done
echo "total is $sum"

14. WAS: to find the sum of 1 to 10 numbers (for loop)

a=1
sum=0
for (( a=1;a<=10;a++ ))
do
sum=`expr $sum + $a`
done
echo "total is $sum"

15. WAS: to find the sum of 1 to 10 numbers (until loop)

a=1
sum=0
until [ $a -ge 10 ]
do
sum=`expr $sum + $a`
a=`expr $a + 1`
done
echo "total is $sum"
16. WAS: to find the factorial of a given no (while loop)

fact=1
echo "enter the no"
read no
while [ $no -gt 0 ]
do
fact=`expr $fact \* $no`
no=`expr $no - 1`
done
echo "factorial is $fact" 

17. WAS: to find the factorial of a given no (for loop)

fact=1
echo "enter the no"
read no
for (( no=no;no>0;no-- ))
do
fact=`expr $fact \* $no`
done
echo "factorial is $fact" 

18. WAS: to find the factorial of a given no (until loop)

fact=1
echo "enter the no"
read no
until [ $no -lt 0 ]
do
fact=`expr $fact \* $no`
no=`expr $no - 1`
done
echo "factorial is $fact" 

19. WAS: to display Fibonacci series (while loop)

a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
while [ $x -gt 0 ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
x=`expr $x - 1`
done     

20. WAS: to display Fibonacci series (for loop)

a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
for (( ;x>0;x-- ))
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
done



21. WAS: to display Fibonacci series (Until loop)

a=0
b=1
echo " how much steps you want "
read no
echo $a
echo $b
x=`expr $no - 2`
until [ $x -lt 0 ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
x=`expr $x - 1`
done         

22. WAS: to get the reverse number of given digit (while loop)

echo "enter the number"
read no
while [ $no -gt 0 ]
do
x=`expr $no % 10`
echo $x
no=`expr $no / 10`
done

23. WAS: to get the reverse number of given digit (for loop)

echo "enter the number"
read no
for (( no=no;no>0;no=no/10 ))
do
x=`expr $no % 10`
echo $x
done

24. WAS: to get the reverse number of given digit (Until loop)

echo "enter the number"
read no
until [ $no -lt 0 ]
do
x=`expr $no % 10`
echo $x
no=`expr $no / 10`
done

25. WAS: to display the multiplication table of a particular number (while loop)

i=1
echo "enter the number for multiplication table"
read no
while [ $i -le 10 ]
do
x=`expr $i \* $no`
echo "$i x $no = $x"
i=`expr $i + 1`
done

26. WAS: to display the multiplication table of a particular number (for loop)

echo "enter the number for multiplication table"
read no
for (( i=1;i<=10;i++ ))
do
x=`expr $i \* $no`
echo "$i x $no = $x"
done

27. WAS: to display the multiplication table of a particular number (until loop)

i=1
echo "enter the number for multiplication table"
read no
until [ $i -ge 10 ]
do
x=`expr $i \* $no`
echo "$i x $no = $x"
i=`expr $i + 1`
done

28. WAS: to display the multiplication table from upto N number (While loop)

i=1
j=1
echo "enter upto wht number you want the tables"
read num
while [ $j -le $num ]
do
while [ $i -le 10 ]
do
x=`expr $i \* $j`
echo " $i x $j = $x"
i=`expr $i + 1`
done
j=`expr $j + 1`
done         

29. WAS: to display the multiplication table from upto N number (While loop)

echo "enter upto wht number u wnt"
read no
for (( j=1;j<=no;j++ ))
do
for (( i=1;i<=10;i++ ))
do
t=`expr $i \* $j`
echo "$i x $j = $t"
done
done  

30. WAS: to display the multiplication table from upto N number (Until loop)

i=1
j=1
echo "enter upto wht number ypu want the tables"
read num
until [ $j -ge $num ]
do
until [$i -ge 10 ]
do
x=`expr $i \* $j`
echo " $i x $j = $x"
i=`expr $i + 1`
done
j=`expr $j + 1`
done         

31. WAS: to do sum of any digit (while loop)

echo "enter any no"
read no
sum=0
while [ $no -gt 0 ]
do
x=`expr $no % 10`
sum=`expr $sum + $x`
no=`expr $no / 10`
done
echo "sum = $sum"

32. WAS: to do sum of any digit (for loop)

echo "enter any no"
read no
sum=0
for (( no=no;no>0;no/=10 ))
do
x=`expr $no % 10`
sum=`expr $sum + $x`
done
echo "sum = $sum"

33. WAS: to do sum of any digit (Until loop)

echo "enter any no"
read no
sum=0
until [ $no -lt 0 ]
do
x=`expr $no % 10`
sum=`expr $sum + $x`
no=`expr $no / 10`
done
echo "sum = $sum"
34. Write a shell script to find even or not.
echo "enter the no"
read no
x=`expr $no % 2`
if [  $x -eq 0 ]
then
echo "even"
else
echo "odd"
fi

35. WAS. To find the given year is leap year or not

echo "enter the year"
read year
x=`expr $year % 4`
if [ $x -eq 0 ]
then
echo "leap year"
else
echo "not leap year"
fi

36. WAS: to find the greatest of all from 3 numbers.

echo "enter three numbers"
read n1
read n2
read n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is greater"
elif [ $n2 -gt $n3 ] && [ $n2 -gt $n1 ]
then
echo "$n2 is greater"
else
echo "$n3 is greater"
fi
36. WAS: to find the greatest of all from 3 numbers.

i=1
max=$1
for i in $*
do
if [ $i -gt $max ]
then
max=$i
fi
done
echo "max = $max"

37. WAS: to find percentage and class of a student.

echo "enter 5 sub marks"
read s1
read s2
read s3
read s4
read s5
tot=`expr $s1 + $s2 + $s3 + $s4 + $s5`
per=`expr $tot \* 100 / 5`
if [ $per -gt 70 ]
then
echo "distinction"
elif [ $per -gt 60 ]
then
echo "first class"
elif [ $per -gt 50 ]
then
echo "second class"
else
echo "third class"
fi



38. WAS: to find it is prime or not

x=0
i=2
echo "enter the no"
read no
while [ $i -lt $no ]
do
y=`expr $no % $i`
if [ $y -eq 0 ]
then
x=1
fi
i=`expr $i + 1`
done
if [ $x -eq 0 ]
then
echo "prime"
else
echo "not prime"
fi       

39. WAS: to find string is equal or not

if test "$1" == "$2"
then
echo "equal"
else
echo "not equal"
fi   

40. WAS: to find check that file exist or not

if test -a f1
then
echo "exist"
else
echo "dosent exist"
fi    
41. WAS: to find that file is readable or not

if test -r f1
then
echo "readable"
else
echo "not readable"
fi  

42. WAS: to find that file is writable or not

if test -w f1
then
echo "writeable"
else
echo "not writeable"
fi  

43. WAS: to find that string is null or not

if test -z $1
then
echo "NULL"
else
echo "NOT NULL"
fi  

44. WAS: to find that file is executable or not

if test -x f1
then
echo "executable"
else
echo "not"
fi   



45. WAS: to find the minimum and maximum from given 5 numbers

i=1
min=$1
max=$1
for i in $*
do
if [ $i -lt $min ]
then
min=$i
fi
if [ $i -gt $max ]
then
max=$i
fi
done
echo "min = $min and max = $max"

46. WAS: to count how many even and odd numbers are there.

i=1
a=0
b=0
for i in $*
do
j=`expr $i % 2`
if [ $j -eq 0 ]
then
a=`expr $a + 1`
fi
if [ $j -eq 1 ]
then
b=`expr $b + 1`
fi
done
echo "even = $a and odd = $b"