Post on 20-Jul-2015
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 1/35
Assignment no.1
H.C.F and L.C.M of two positive integers
Program Code :-
echo -n "Enter 1st no.-- "
read a
echo -n "Enter 2nd no.-- "
read c
m=$a
n=$c
d=` expr $m \* $n`
while [ $m -ne $n ]
do
if [ $m -gt $n ]; then
m=` expr $m - $n`
else
n=` expr $n - $m`
fi
done
e=` expr $d / $m`
echo "LCM of $a and $c is $e"
echo "GCD of $a and $c is $m"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 2/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh lcmgcd.sh
Enter 1st no.-- 20
Enter 2nd no.-- 26
LCM of 20 and 26 is 260
GCD of 20 and 26 is 2
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 3/35
Assignment no.2
First N Fibonacci number
Program code:-
echo -n "Enter the term: "
read n
a=0
c=1
if [ $n -eq 0 ]; then
echo "NO TERM TO DISPLAY OF THE SERIES"
fi
if [ $n -eq 1 ]; then
echo "::::::::::THE FIBONACCI SERIES IS BELOW::::::::::"
printf "\t\t\t$a\n"
fi
if [ $n -gt 2 ]; then
echo "::::::::::THE FIBONACCI SERIES IS BELOW::::::::::"
printf "\t\t\t$a\n"
printf "\t\t\t$c\n"
fi
m=` expr $n - 2`
while [ $m -gt 0 ]
do
d=` expr $a + $c`
a=$c
c=$d
m=` expr $m - 1`
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 4/35
printf "\t\t\t$d\n"
done
if [ $m -eq 0 ]
then
echo "::::::::::::::::END OF THE SERIES::::::::::::::::"
fi
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh fibonacci.sh
Enter the term: 9
::::::::::THE FIBONACCI SERIES IS BELOW::::::::::
0
1
1
2
3
5
8
13
21
::::::::::::::::END OF THE SERIES::::::::::::::::
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 5/35
Assignment no.3
Maximum, minimum, sum and average among N given value
Program code:-
echo -n "Enter no. of numbers you want to insert:--"
read n
echo -n "Enter 1st no. "
read a
max=$a
min=$a
sum=$a
m=` expr $n - 1`
i=1
while [ $m -ge $i ]
do
echo -n "Enter next no. "
read c
sum=` expr $sum + $c`
if [ $max -lt $c ]
then
max=$c
fi
if [ $min -gt $c ]
then
min=$c
fi
i=` expr $i + 1`
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 6/35
done
d=` expr $sum / $n`
printf "maximum no is $max\nminimum no is $min\n"
printf "Sum is $sum\naverage is $d\n"
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh mmsa.sh
Enter no. of numbers you want to insert:--5
Enter 1st no. 98
Enter next no. 56
Enter next no. 78
Enter next no. 12
Enter next no. 65
maximum no is 98
minimum no is 12
Sum is 309
average is 51
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 7/35
Assignment no.4
Check a number is prime or not
Program code:-
echo -n "Enter the no. to check it is prime or not:-- "
read n
i=2
m=` expr $n / 2`
printf "\nAFTER CHECKING THE RESULT IS BELOW\n"
while [ $i -le $m ]
do
j=` expr $n % $i`
if [ $j -eq 0 ];then
printf "\n"
echo "$n is not prime as it is first divided by $i"
i=$n
else
i=` expr $i + 1`
fi
done
if [ $i -ne $n ];then
printf "\n"
echo "$n is prime as it is divided only by 1 and $n"
fi
printf "\n"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 8/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh primecheck.sh
Enter the no. to check it is prime or not:-- 35
AFTER CHECKING THE RESULT IS BELOW
35 is not prime as it is first divided by 5
[Pritam-Project@mukherjee ~]$ sh primecheck.sh
Enter the no. to check it is prime or not:-- 97
AFTER CHECKING THE RESULT IS BELOW
97 is prime as it is divided only by 1 and 97
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 9/35
Assignment no.5
Check a string is palindrome or not
Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO CHECK A STRING IS PALINDROME
OR NOT"
echo -n "Enter a string to check it: "
read str
len=`echo $str|wc -c`
l=$len
printf "\n"
printf "::::::AFTER CHECKING THE RESULT IS BELOW:::::::\n"
echo -n "THE ENTERED STRING "
for((i=1;i<=`expr $len/2`;i++))
do
l=`expr $l - 1`
a=`echo $str|cut -c $i`
b=`echo $str|cut -c $l`
if [ $a != $b ]
then
echo "IS NOT PALINDROME "
exit 1
fi
done
echo "IS PALINDROME"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 10/35
INPUT OUTPUT
Pritam-Project@mukherjee ~]$ sh strpallin.sh
THIS PROGRAM IS IMPLEMENTED TO CHECK A STRING IS PALINDROME OR NOT
Enter a string to check it: madam
::::::AFTER CHECKING THE RESULT IS BELOW:::::::
THE ENTERED STRING IS PALINDROME
Pritam-Project@mukherjee ~]$ sh strpallin.sh
THIS PROGRAM IS IMPLEMENTED TO CHECK A STRING IS PALINDROME OR NOT
Enter a string to check it: mukherjee
::::::AFTER CHECKING THE RESULT IS BELOW:::::::
THE ENTERED STRING IS NOT PALINDROME
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 11/35
Assignment no. 6
Find the length of a given string
Program code:-
echo ":::THIS PROGRAM IS IMPLEMENTED TO COUNT THE STRING
LENGTH:::"
echo -n "Enter a string to count length: "
read str
len=`echo $str|wc -c`
len=` expr $len - 1`
printf "\nThe length of the entered string is $len\n"
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh strlength.sh
:::THIS PROGRAM IS IMPLEMENTED TO COUNT THE STRING LENGTH:::
Enter a string to count length: abchyegdth
The length of the entered string is 10
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 12/35
Assignment no. 7
Reverse a string i.e. RAM IS A GOOD BOY is displayed as BOY GOOD
A IS RAM
Program code:-
echo -n "Enter the string:--- "
read a
echo ""
b=` echo "$a"|rev`
i=1
c=` echo "$b"|cut -d" " -f$i|rev `
echo "AFTER REVERSING THE STRING IS:----"
echo ""
while [ -n "$c" ]
do
printf "$c "
i=` expr $i + 1`
c=` echo "$b"|cut -d" " -f$i|rev `
done
printf "\n"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 13/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh revstr.sh
Enter the string:--- RAM IS A GOOD BOY
AFTER REVERSING THE STRING IS:----
BOY GOOD A IS RAM
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 14/35
Assignment no.8
File name as command line argument and searches and outputs
whether it exists. If it exists then RWX permission will be
displayed
Program code:-
file=$1
ls -l > temp
str=`grep "$1" temp`
if [ $? -eq 0 ]
then
echo "::::::::::::::The file exists::::::::::::::"
printf "The permissions of the file are:"
str1=`ls -l $1|cut -f1 -d" "`
echo "$str1"
else
echo "The file dose not exists"
echo "So, no permissions to display"
fi
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 15/35
INPUT OUTPUT
Pritam-Project@mukherjee ~]$ sh filesearch.sh local
::::::::::::::The file exists::::::::::::::
The permissions of the file are:-rw-rw-r--.
[Pritam-Project@mukherjee ~]$ sh filesearch.sh test1
The file dose not exists
So, no permissions to display
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 16/35
Assignment no.9
sort N integers
Program code:-
echo "-----THIS PROGRAM IS IMPLEMENTED FOR SORTING IN ASSINDING
ORDER OF ARRAY ELEMENT-----"
echo -n "Enter the no. of elements for the array:-"
read n
for((i=0;i<n;i++))
do
read -p "a[$i]:-" a[$i]
done
for((i=0;i<n;i++))
do
k=` expr $n - $i - 1`
for((j=0;j<k;j++))
do
l=` expr $j + 1`
if [ $[a[$j]] -gt $[a[$l]] ]
then
t=$[a[$j]]
a[$j]=$[a[$l]]
a[$l]=$t
fi
done
done
echo "::::::AFTER SORTING::::::"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 17/35
echo "THE ARRAY ELEMENTS ARE:-"
for((i=0;i<n;i++))
do
printf "\t$[a[$i]]\n"
done
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh arraysort.sh
-----THIS PROGRAM IS IMPLEMENTED FOR SORTING IN ASSINDING ORDER OF
ARRAY ELEMENT-----
Enter the no. of elements for the array:-6
a[0]:--4
a[1]:-2
a[2]:--9
a[3]:-5
a[4]:--7
a[5]:-6
:::::AFTER SORTING::::::
THE ARRAY ELEMENTS ARE:-
-9
-7
-4
2
5
6
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 18/35
Assignment no.10
value of the series 1!+2!+3!+.......+n!
Program code:-
fact()
{
f=1
for((j=1;j<=$1;j++))
do
f=` expr $f \* $j`
done
return $f
}
echo -n "Enter the term :-"
read n
s=0
echo "AFTER CALCULATION THE RESULT IS------"
for((i=1;i<=n;i++))
do
fact $i
c=$?
s=` expr $s + $c`
if [ $i -ne $n ];then
printf "$i!+"
else
printf "$i!="
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 19/35
fi
done
echo " $s "
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh sumfact.sh
Enter the term :-5
AFTER CALCULATION THE RESULT IS------
1!+2!+3!+4!+5!= 153
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 20/35
Assignment no.11
Check a given user name is valid or not
Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOT"
echo -n "Enter The USER NAME:-"
read user
who>a
f=0
while read line
do
u=` echo "$line"|cut -d " " -f1`
if [ "$u" = "$user" ]
then
f=1
break
fi
done<a
echo "AFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:-"
if [ $f -eq 1 ]
then
echo "The user name is valid"
else
echo "The user name is not valid"
fi
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 21/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh user.sh
THIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOT
Enter The USER NAME:-Pritam-Project
AFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:-
The user name is valid
[Pritam-Project@mukherjee ~]$ sh user.sh
THIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOT
Enter The USER NAME:-mukherjee
AFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:-
The user name is not valid
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 22/35
Assignment no.12
print prime numbers within a range Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO FIND PRIME NUMBERS IN A GIVEN
RANGE"
echo -n "Enter the LOWER BOUND::"
read lb
echo -n "Enter the UPPER BOUND::"
read ub
if [ $ub -eq $lb ] ;then
echo "There is no range"
exit 1
fi
if [ $lb -eq 1 ] ;then
lb=` expr $lb + 1`
fi
f=0
for((i=lb;i<=$ub;i++)) ;do
for((j=2;j<$i;j++)) ;do
f=0
a=` expr $i % $j`
if [ $a -eq 0 ] ; then
f=1
break
fi
done
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 23/35
if [ $f -eq 0 ] ;then
echo " Prime nos. in the given range are: "
printf "$i "
fi
done
INPUT OUTPUT
THIS PROGRAM IS IMPLEMENTED TO FIND PRIME NUMBERS IN A GIVEN
RANGE
Enter the LOWER BOUND::1
Enter the UPPER BOUND::90
PRIME NOS. IN THE GIVEN RANGE ARE:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 24/35
Assignment no.13
write a positive integer in the reverse order
Program code:-
echo "::::::THIS PROGRAM IS IMPLEMENTED TO REVERSE AN INTEGER::::::"
echo -n "Enter the integer to reverse:-"
read n
c=` echo $n|rev`
echo ""
echo -n "After the reverse, the result is :-"
echo $c
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh intrev.sh
::::::THIS PROGRAM IS IMPLEMENTED TO REVERSE AN INTEGER::::::
Enter the integer to reverse:-9546215
After the reverse, the result is :-5126459
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 25/35
Assignment no.14
sum of the digits of a given number
Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO CALCULATE THE SUM OF THE
DIGIT OF AN INTEGER"
echo -n "Enter the no. "
read n
a=$n
sum=0
while [ $a -gt 0 ]
do
c=` expr $a % 10`
a=` expr $a / 10`
sum=` expr $sum + $c`
if [ $a -gt 0 ]
then
printf "$c+"
else
printf "$c="
fi
done
echo $sum
echo "The sum of the digit of $n is $sum"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 26/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh sumdigit.sh
THIS PROGRAM IS IMPLEMENTED TO CALCULATE THE SUM OF THE DIGIT OF
AN INTEGER
Enter the no. 644789
9+8+7+4+4+6=38
The sum of the digit of 644789 is 38
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 27/35
Assignment no.15
print the grade of a student based on average obtained from
the total marks of 4 subjects
Program code:-
echo -n "Enter Student name :-"
read name
echo "Four subjects are :-"
read -p "SUBJECT1 : " sub1
read -p "SUBJECT2 : " sub2
read -p "SUBJECT3 : " sub3
read -p "SUBJECT4 : " sub4
echo "Enter the marks of the subjects:-"
read -p "$sub1=" m1
read -p "$sub2=" m2
read -p "$sub3=" m3
read -p "$sub4=" m4
echo -n "Total marks of the 4 subjects is $m1+$m2+$m3+$m4="
m=` expr $m1 + $m2 + $m3 + $m4`
echo $m
echo ""
echo -n "Average marks is :-"
avg=` expr $m / 4`
echo $avg
echo ""
if [ $avg -ge 90 ];then
echo "$name obtained GRADE 'A+'"
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 28/35
elif [ $avg -ge 80 ];then
echo "$name obtained GRADE 'A'"
elif [ $avg -ge 70 ];then
echo "$name obtained GRADE 'B+'"
elif [ $avg -ge 60 ];then
echo "$name obtained GRADE 'B'"
elif [ $avg -ge 50 ];then
echo "$name obtained GRADE 'C'"
elif [ $avg -ge 40 ];then
echo "$name obtained GRADE 'D'"
else
echo "$name failed the exam"
fi
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 29/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh grade.sh
Enter Student name :-Pritam Mukherjee
Four subjects are :-
SUBJECT1 : Computer
SUBJECT2 : Mathematics
SUBJECT3 : Physics
SUBJECT4 : English
Enter the marks of the subjects:-
Computer=75
Mathamatics=90
Physics=65
English=75
Total marks of the 4 subjects is 75+90+65+75=305
Average marks is :-76
Pritam Mukherjee obtained GRADE 'B+'
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 30/35
Assignment no.16
print the current date and time
Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO SHOW THE CURRENT DATE AND
TIME"
echo -n "CURRENT DATE IS :-"
echo `date +%D`
echo -n "CURRENT TIME IS :-"
echo ` date +%T`
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh datetime.sh
THIS PROGRAM IS IMPLEMENTED TO SHOW THE CURRENT DATE AND TIME
CURRENT DATE IS :-12/03/11
CURRENT TIME IS :-11:22:40
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 31/35
Assignment no.17
sum of individual row element and column element of 3*3
matrix input
Program code:-
echo -n "Enter the number of row or column: "
read row
declare -a array
rowSquare=`expr $row \* $row`
adjustment=0
for (( i = 0; i < $row; i++ ))
do
k=0
for (( j = 0; j < $rowSquare; j += $row ))do
index=`expr $i + $j + $adjustment`
adjustment=`expr $adjustment - $row + 1`
echo -n "Enter the value of A`expr $i + 1``expr $k + 1`: "
read array[index]
k=`expr $k + 1`
done
adjustment=`expr $adjustment + $rowSquare - 1`
done
echo
echo "The given matrix is: "
adjustment=0
for (( i = 0; i < $row; i++ ))
do
for (( j = 0; j < $rowSquare; j += $row ))
do
index=`expr $i + $j + $adjustment`
adjustment=`expr $adjustment - $row + 1`
echo -n "${array[index]} "
done
adjustment=`expr $adjustment + $rowSquare - 1`echo
done
adjustment=0
for (( i = 0; i < $row; i++ ))
do
colSum=0
rowSum=0
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 32/35
for (( j = 0; j < $rowSquare; j += $row ))
do
colIndex=`expr $i + $j`
colSum=`expr $colSum + ${array[colIndex]}`
rowIndex=`expr $i + $j + $adjustment`
rowSum=`expr $rowSum + ${array[rowIndex]}`adjustment=`expr $adjustment - $row + 1`
done
adjustment=`expr $adjustment + $rowSquare - 1`
printf "Sum of `expr $i + 1`th column: $colSum\n"
printf "Sum of `expr $i + 1`th row: $rowSum"
done
echo -n "Press enter to continue or any other key to exit..."
read key
exit 0
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 33/35
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh matrix.sh
Enter the number of row or column: 3
Enter the value of A11: 1
Enter the value of A12: 2
Enter the value of A13: 3
Enter the value of A21: 4
Enter the value of A22: 5
Enter the value of A23: 6
Enter the value of A31: 7
Enter the value of A32: 8
Enter the value of A33: 9
The given matrix is:
1 2 3
4 5 6
7 8 9
Sum of 1th column: 12
Sum of 1th row: 6
Sum of 2th column: 15
Sum of 2th row: 15
Sum of 3th column: 18
Sum of 3th row: 24
Press enter to continue or any other key to exit...
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 34/35
Assignment no.18
execute good morning, good afternoon, good evening and good
night as per the system time
Program code:-
time=`date |tr -s ' '|cut -d" " -f4 | cut -d":" -f1`
if [ $time -gt 0 -a $time -le 12 ]; then
printf "\n good morning"
fi
if [ $time -gt 12 -a $time -le 17 ]; then
printf "\n good afternoon"
fi
if [ $time -gt 17 -a $time -le 20 ]; then
printf "\n good evening"
fi
if [ $time -gt 20 -a $time -le 24 ];then
printf "\ngood night"
fi
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh time.sh
good morning
5/17/2018 Unix Final Project - slidepdf.com
http://slidepdf.com/reader/full/unix-final-project 35/35
Assignment no.19
check a given user name is login in the system or not
Program code:-
echo "THIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOT"echo -n "Enter The USER NAME:-"read userwho>a f=0while read line do u=` echo "$line"|cut -d " " -f1`if [ "$u" = "$user" ]then
f=1break
fidone<aecho "AFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:-"
if [ $f -eq 1 ]then
echo "The user is logged in"else
echo "The user is not logged in"fi
INPUT OUTPUT
[Pritam-Project@mukherjee ~]$ sh chklogin.sh
THIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOTEnter The USER NAME:-Pritam-ProjectAFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:- The user is logged in
[Pritam-Project@mukherjee ~]$ sh chklogin.shTHIS PROGRAM IS IMPLEMENTED TO CHECK A USER IS VALID OR NOT
Enter The USER NAME:-mukherjeeAFTER CHECKING USER LIST FROM 'WHO' COMMAND, THE RESULT IS:-The user is not logged in