Unix Ass 2
Unix Ass 2
1. Write a shell program that will accept an integer value as runtime input and print the
even numbers between 1 to that number.
2. Write a shell program that will accept an integer value as run time input and will
check if it is prime or not.
3. Write a shell program that will accept an integer value as run time input and will
check if it is perfect number or not.
4. Write a shell Program that will accept an integer value as command line argument and
will perform the following operations and will print result;
(a) Compute the sum of all even numbers present in between them,
(b) Compute the product of all odd numbers present in between them.
-------------------------------------------------------------------------------
#ASSIGNMENT 2.1
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-1.sh: A shell program that will accept an integer value as runtime input
and print the even numbers from 1 to that number.
c=1
echo "All even values between 1 to $a: "
while [ $c -lt $a ]; do
if [ `expr $c % 2` -eq 0 ]; then
echo -e "$c \c"
fi
c=`expr $c + 1`
done
echo -e "\n"
#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-1.sh
[user1@localhost ~]$ chmod +x Ass2-1.sh
[user1@localhost ~]$ ./Ass2-1.sh
Enter a integer number: 20
All even values between 1 to 20:
2 4 6 8 10 12 14 16 18
-------------------------------------------------------------------------------
#ASSIGNMENT 2.2
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-2.sh: A shell program that will accept an integer value as run time input
and will check if it is prime or not.
c=1
while [ $c -le `expr $a / 2` ]; do
if [ `expr $a % $c` -eq 0 ]; then
temp=1
break
fi
c=`expr $c + 1`
done
#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-2.sh
[user1@localhost ~]$ chmod +x Ass2-2.sh
[user1@localhost ~]$ ./Ass2-2.sh
Enter a integer number: 69
69 is not a Prime Number.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
#ASSIGNMENT 2.3
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-3.sh: A shell program that will accept an integer value as run time input
and will check if it is perfect number or not.
c=1
res=0
while [ $c -lt $a ]; do
if [ `expr $a % $c` -eq 0 ]; then
res=`expr $res + $c`
fi
c=`expr $c + 1`
done
#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-3.sh
[user1@localhost ~]$ cnmod +x Ass2-3.sh
[user1@localhost ~]$ ./Ass2-3.sh
Enter a integer number: 6
6 is a Perfect Number.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
#ASSIGNMENT 2.4
-------------------------------------------------------------------------------
#!/bin/sh
#Ass2-4.sh: A shell Program that will accept an integer value as command line
argument and will perform the following operations and will print result; (a)
Compute the sum of all even numbers present in between them, (b) Compute the
product of all odd numbers present in between them.
if [ $# -ne 1 ]; then
echo "Please Enter Your Value as Command Line Agrument..."
exit 1
fi
echo "The Value Entered: $1"
c=1
even=0
odd=1
while [ $c -lt $1 ]; do
if [ `expr $c % 2` -eq 0 ]; then
even=`expr $even + $c`
else
odd=`expr $odd \* $c`
fi
c=`expr $c + 1`
done
#END OF PRG
-------------------------------------------------------------------------------
#OUTPUT:
[user1@localhost ~]$ vi Ass2-4.sh
[user1@localhost ~]$ chmod +x Ass2-4.sh
[user1@localhost ~]$ ./Ass2-4.sh 20
The Value Entered: 20
Sum of all Even numbers present between 1 to 20: 90
Product of all Odd numbers present between 1 to 20: 654729075
-------------------------------------------------------------------------------