Unix Lab Programs: Exercise No:2. ODD OR EVEN NUMBERS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

UNIX LAB PROGRAMS

Exercise no:2. ODD OR EVEN NUMBERS


$ cat odd
echo -n "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

OUTPUT
$ sh odd
Enter numnber : 3
3 is odd number
$sh odd
Enter numnber : 2
2 is even number

Exercise no:3: POSITIVE OR NEGATIVE


$ cat pos
echo "Positive or negative"
echo "Enter the number"
read n
if test $n -gt 0
then
echo "$n is a positive number"
elif test $n -lt 0
then
echo "$n is a negative number"
else
echo "$n is equal to zero"
fi
OUTPUT:
$ sh pos
Positive or negative
Enter the number
1
1 is a positive number
-9
-9 is a negative number
0
0 is equal to zero
Exercise no:4: BIGGEST OF THREE NUMBERS
$ cat big
echo -n "Please enter three numbers (separate number by space) : "
read a b c
# compare a with b and c. Note -a is logical and operator
if [ $a -gt $b -a $a -gt $c ]
then
big=$a
elif [ $b -gt $a -a $b -gt $c ] # compare b with a and c
then
big=$b
elif [ $c -gt $a -a $c -gt $b ] # compare c with a and b
then
big=$c
elif [ $a -eq $b -a $a -eq $c -a $b -eq $c -a $c -eq $b ] # see if all of them a
re equal or not
then
big="All three numbers are same (equal)"
else # something must be wrong if we are here, like one of number is character
such as 'A'
big="Can not guess greaters of three numbers"
fi
# display result
echo "Result : $big"

OUTPUT
$ sh big
Please enter three numbers (separate number by space) : 1 2 3
Result : 3
$sh big
Please enter three numbers (separate number by space) : 2 3 d
Result : Can not guess greaters of three numbers
EXERCISE NO 5 SQUARE AND CUBE OF FIVE NUMBERS
$ cat > cube
echo " cube and square of first three numbers are"
for i in 1 2 3 4 5
do
sq=`expr $i '*' $i`
cube=`expr $sq '*' $i`
echo "$i $sq $cube"
done

OUTPUT

[mech938@localhost mech938]$ sh cube


cube and square of first three numbers are
111
248
3 9 27
4 16 64
5 25 125

Exercise no: 6a. FACTORIAL OF THREE NUMBERS

$ cat fact
echo "Enter a number: "
read num
i=2
res=1
if [ $num -ge 2 ]
then
while [ $i -le $num ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi
echo "Factorial of $num = $res"
num = $res"

OUTPUT :

$ sh fact
Enter a number:3
Factorial of 3=6
Exercise no: 6B:FIBBANACI SERIES
$ cat fib
echo "Enter How many numbers:"
read num
num1=0
num2=1
echo "Fibonacci series:"
echo $num1
echo $num2
count=2
while [ $count -le $num ]
do
num3=`expr $num1 + $num2`
echo $num3
num1=$num2
num2=$num3
count=`expr $count + 1`
done

Output :
$ sh fib
Enter How many numbers:
3
Fibonacci series:
0
1
1
Exercise no: 6C:PALINDROME OF A NUMBER
$ cat pal
echo -n "Enter number : "
read n
# store single digit
sd=0
# store number in reverse order
rev=""
# store original number
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi

OUTPUT$ sh pal
Enter number : 333
Number is palindrome
[mohana@localhost mohana]$ sh pal
Enter number : 123
Number is NOT palindrome

Exercise no: 7: PROGRAM USING CASE $ cat case


echo "MENU\N"
echo "1.Todays date "
echo "2.List of users "
echo "3.users of system "
echo "4.To know the terminal name "
echo "5.quit"
echo "enter your option(1-5)"
read ch
case "$ch" in
1) date;;
2) ls -l;;
3) who;;
4) tty;;
5) exit;;
OUTPUT$ sh case
MENU\N
1.Todays date
2.List of users
3.users of system
4.To know the terminal name
5.quit
enter your option(1-5)
4
/dev/pts/8

Exercise no:8 SHELL SCRIPT USING GREP FILTER COMMAND


$ cat grep
if (who | grep $1 > /dev/null )
then
echo "$user is logged on"
else
echo "$user is not logged on"
fi

OUTPUT:$ sh grep mech938


is not logged on

EXERCISE NO 9:SHELL SCRIPT USING SED EDITOR


$ cat > sed
hi this is praveen
i`m from k.r.c.e.
this college is very nice
i love my college very much
thankyou

$ sed 's/k.r.c.e./m.a.m.c.e./' sed


$ cat > sed
hi this is praveen
i`m from m.a.m.c.e.
this college is very nice
i love my college very much
thankyou
EXERCISE NO 10 : SHELL SCRIPT USING AWK COMMAND
$ cat awk1
BEGIN {
myprompt = "(To Stop press CTRL+D) > "
printf "Welcome to MyAddtion calculation awk program v0.1\n"
printf "%s" ,myprompt
}
{
no1 = $1
op = $2
no2 = $3
ans = 0
if ( op == "+" )
{
ans = $1 + $3
printf "%d %c %d = %d\n" ,no1,op,no2,ans
printf "%s" ,myprompt
}
else
{
printf "Opps!Error I only know how to add.\nSyntax: number1 + number2\n"
printf "%s" ,myprompt
}
}
END {
printf "\nGoodbuy %s\n" , ENVIRON["USER"]
}

OUTPUT

$sh awk1
Welcome to MyAddtion calculation awk program
2+3
2+3=4
EXERCISE NO11:SHELL SCRIPT USING SECURITY COMMAND
$ cat > sec
echo" enter a file name"
read fname
ls -l $fname
echo " do u want to xecute"
read ch
if [ $ch = y]
then
chmod 777 $fname
echo " permission given"
ls -l $fname
else
echo"permission not given"
fi

EXERCISE NO 12 : PROGRAM USING FORK


include<unistd.h>
int main()
{
int pid;
printf("hello world\n");
pid = fork();
if(pid != 0)
printf("i'm the father and my son's PID is %d\n”,pid);
else
printf("i'm the son\n");
printf("good bye and thank u\n");
}
EXERCISE NO 13: PROGRAM FOR SIGNAL HANDLING
#include<signal.h>
#include<stlib.h>
#include<sys/types.h>
#include<unstd.h>
void alarm_handler(int sig_num)
{
printf("welcome back after four seconds");
exit(0);
}
int main()
{
int time=1;
printf("message appears in four seconds\n");
siglan(SIGALRM,alarm_handler);
alarm(time*4);
pause();
return 0;
}

OUTPUT
$ cc signal.c
$./a.out
message appears in four seconds
Welcome back after four seconds

EXERCISE NO 14: PROGRAM FOR FILE HANDLING

#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
char filename[40] ;
printf("Enter file to be displayed: ");
gets( filename ) ;
fp = fopen( filename, "r");
c = getc( fp ) ;
while ( c != EOF )
{
putchar(c);
c = getc ( fp )
}
fclose( fp );
}

You might also like