0% found this document useful (0 votes)
144 views20 pages

UNIX ASSIGNMENT (1) 20 Pages

This document contains 34 programming assignments for shell scripting. The assignments cover a wide range of shell scripting concepts including: - Getting input from users and displaying output - Performing arithmetic, string, and conditional operations - Using loops (for, while) and flow control structures (if/else, case) - Working with files, directories and file properties - Implementing functions like prime number check, Armstrong number check, Fibonacci series etc. The assignments start with basic concepts and increase in complexity, touching on most common tasks done using shell scripts.

Uploaded by

anushka gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views20 pages

UNIX ASSIGNMENT (1) 20 Pages

This document contains 34 programming assignments for shell scripting. The assignments cover a wide range of shell scripting concepts including: - Getting input from users and displaying output - Performing arithmetic, string, and conditional operations - Using loops (for, while) and flow control structures (if/else, case) - Working with files, directories and file properties - Implementing functions like prime number check, Armstrong number check, Fibonacci series etc. The assignments start with basic concepts and increase in complexity, touching on most common tasks done using shell scripts.

Uploaded by

anushka gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIX

ASSIGNMENT

1 NAVIN KUMAR MISHRA


#1. Write a shell script program to enter a number from the user and display
a number.

echo “Enter the a number”


read x
echo “x = “ $x

#2. Write a shell program to Print Student Name and Marks of three students
that is enter by users.

echo "Enter 3 Students Names & Marks : "


read n1
read m1
read n2
read m2
read n3
read m3
echo "Name" " " "Marks"
echo $n1 " " $m1
echo $n2 " " $m2
echo $n3 " " $m3

#3. Write a shell script program to perform all Arithmetic operations on two
integers which is input by users.

echo "enter the first no."


read a
echo "enter the 2nd no."
read b
c=`expr $a + $b`
echo "sum : "$c
c=`expr $a - $b`
echo "diff : "$c
c=`expr $a \* $b`
echo "mul : "$c
2 NAVIN KUMAR MISHRA
c=`expr $a / $b`
echo "div : "$c
c=`expr $a % $b`
echo "modulus : "$c

#4. Write a shell program to perform all Arithmetic operations on floating


point of two float value which is input by users.

echo "Enter the value of a"


read a
echo "Enter the value of b"
read b
c=`echo $a + $b|bc`
echo "sum : "$c
c=`echo $a - $b|bc`
echo "diff : "$c
c=`echo $a \* $b|bc`
echo "mul : "$c
c=`echo $a / $b|bc`
echo "div : "$c
c=`echo $a % $b|bc`
echo "modulus : "$c

#5. Write the shell program to Enter the Principal , Rate and Time from the
user and find the simple interest.

clear
echo "Enter P,N and R"
read p
read n
read r
si=`expr \( $p \* $n \* $r \) / 100`
echo "Simple Interest is " $si

#6. Write a shell script program to find area of circle, Rectangle and Square.

3 NAVIN KUMAR MISHRA


echo “Enter radius”
read r
echo "Enter Length & Breadth"
read l
read b
echo "Enter side"
read s
a=`echo $r \* $r \* 3.14|bc`
echo “Area of Circle ” $a
a=`echo $l \* $b`
echo “Area of Rectangle ” $a
a=`echo $s \* $s`
echo “Area of Square ” $a

#7. Write a shell script program to print our Address ‘n’ times by users.

echo "Enter n : "


read n
echo "Enter Your Plot No"
read plot
echo "Enter u r Street Name"
read street
echo "Enter u r city"
read city
echo "Printing Address " $n " times :"
for ((i=0;i
do
echo $plot " , " $street " , " $city
done

#8. Write a shell script program to find whether the given number is even or
odd input by users.

4 NAVIN KUMAR MISHRA


clear
echo "Enter n"
read n
if [ `expr $n % 2` -eq 0 ]
then
echo "n is even"
else
echo "n is odd"
fi

#9. Write a shell script program to find whether number is positive , negative
or zero.

clear
echo "Enter n"
read n
if [ $n -gt 0 ]
then
echo "n is positive"
else
if [ $n -eq 0 ]
then
echo "n is zero"
else
echo "n is negative"
fi
fi

#10. Write a shell script program to enter the three numbers from user and
find the Greatest of three numbers.

clear
echo "Enter any 3 numbers"
read x
read y
5 NAVIN KUMAR MISHRA
read z
if [ $x -gt $y -a $x -gt $z ]
then
echo $x " is Great"
else
if [ $y -gt $z ]
then
echo $y " is Great"
else
echo $z " is Great"
fi
fi

#11. Write a shell script program to enter the year and find whether year is
leap year or not.

echo "Enter the year"


read y
r=`expr $y % 4`
if [ $r -eq 0 ]
then
echo $r " is a Leap Year"
else
echo $r " is not a Leap Year"
fi

#12. Write a shell script program to enter a number and check whether
number is divisible by 11 or not.

echo "Enter any Number"


read n
r=`expr $y % 11`
if [ $r -eq 0 ]
then
echo $r " is divisible by 11"
else
6 NAVIN KUMAR MISHRA
echo $r " is not divisible by 11"
fi
#13. Write a shell script program to print natural numbers from 1 to 10 using
WHILE loop.

echo "1-10 Natural No's are :"


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

#14. Write a shell script program to print perfect numbers from 1 to 100

echo "Perfect Numbers 1 - 100 are : "


n=1
while [ $n -lt 100 ]
do
x=$n
sum=0
for ((i=1;i
do
r=`expr $x % $i`
if [ $r = 0 ]
then
sum=`expr $sum + $i`
fi
done
if [ $sum -eq $n ]
then
echo $n
fi
n=`expr $n + 1`
done

7 NAVIN KUMAR MISHRA


#15. Write a shell script program to enter a number and print reverse that
number.

clear
echo "Enter a Number"
read n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s \* 10 + $r`
n=`expr $n / 10`
done
echo "Reverse of it is" $s

#16. To find sum of digits of a number

echo "Enter the number"


read n
sum=0
while [ $n -gt 0 ]
do
a=`expr $n % 10`
sum=`expr $sum + $a`
n=`expr $n / 10`
done
echo "The sum of digits of no. is : "$sum

#17. Write a shell script program to print multiplication table using FOR loop
of any number.
echo "Enter the number"
read n
echo "Multiplication Table"
for ((i=1;i<10;i++))
8 NAVIN KUMAR MISHRA
do
echo $n " * " $i " = " `expr $n \* $i`

done

Output :
Enter the number
34
Multiplication Table
34 * 1 = 34
34 * 2 = 68
34 * 3 = 102
34 * 4 = 136
34 * 5 = 170
34 * 6 = 204
34 * 7 = 238
34 * 8 = 272
34 * 9 = 306
34 * 10 = 340

#18. Write a shell script program to print prime numbers between 1 to 20.

echo "Prime numbers 1-20 are : "


j=1
while [ $j -lt 20 ]
do
i=1
c=0
while [ $i -lt $j ]
do
r=`expr $n % $i`
if [ $r -eq 0 ]
then
c=`expr $c + 1`
fi
i=`expr $i + 1`
9 NAVIN KUMAR MISHRA
done
if [ $c -eq 0 ]
then
echo $j
fi
j=`expr $j + 1`
done

#19. Write a shell script program to enter a number from user and find
whether number is prime or not

echo "Enter n"


read n
i=1
c=0
while [ $n -gt $i ]
do
r=`expr $n % $i`
if [ $r -eq 0 ]
then
c=`expr $c + 1`
fi
i=`expr $i + 1`
done
if [ $c -eq 0 ]
then
echo "It is a prime"
else
echo "It is not a prime"
fi

#20. Write a shell script program to enter a number and check whether
number is Armstrong or not

echo "Enter a no"


read n
10 NAVIN KUMAR MISHRA
sum=0
r=0
x=$n
while [ $x -gt 0 ]
do
r=`expr $n % 10`
sum=`expr $sum + $r \* $r \*r`
x=`expr $n / 10`
done
if [ $n -eq $sum ]
then
echo $n " is Armstrong"
else
echo $n " is not Armstrong"
fi

#21. Write a shell script program to print Armstrong numbers from 1 to 1000

echo "Armstrong Numbers 1 - 100 are : "


n=1
while [ $n -lt 1000 ]
do
x=$n
sum=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
sum=`expr $sum + $r \* $r \* $r`
x=`expr $x / 10`
done
if [ $sum -eq $n ]
then
echo $n
fi
n=`expr $n + 1`
done
11 NAVIN KUMAR MISHRA
#22. Write a shell script program to enter a number and find Factorial of that
number.

clear
echo "Enter n"
read n
f=1
while [ $n -gt 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "Factorial is " $f

#23. Write a shell script program to enter the limit of number and print the
Fibonacci series.

echo "Enter limit :"


read n
f1=0
f2=1
echo "The Fibonacci sequence is : "
for (( i=0;i<=n;i++ ))
do
echo $f1
temp=$f2
f2=`expr $f1 + $f2`
f1=$temp
done

#24. Write a shell script program to print Employee Payroll.

echo “Enter Basic Salary”


12 NAVIN KUMAR MISHRA
read basic
da=`echo 0.1 \* $basic|bc`
hra=`echo 1.2 \* $basic|bc`
pf=`echo 0.8 \* $basic|bc`
gross=`echo $basic + $da + $hra|bc`
net=`echo $gross - $pf|bc`
echo “DA is” $da
echo “HRA is” $hra
echo “PF is” $pf
echo “Gross Salary is” $gross
echo “Net Salary is” $net

#25. Write a shell script program to implement Break statement.

declare -a num[]
echo "Enter length of list"
read n
i=0
echo "Enter list of Numbers"
for ((i=0;i
do
read num[$i]
done
echo "Sum till a negative no : "
s=0
for ((i=0;i
do
if [ num[$i] -gt 0 ]
then
s=`expr $s + $num[$i]`
else
break
fi
done
echo $s
13 NAVIN KUMAR MISHRA
#26. Write a shell script program to enter the digit from a number print the
corresponding day Using else if ladder.

echo “Enter day no.”


read d
if [ $d –eq 1 ]
then
echo “Sunday”
else
if [ $d –eq 2 ]
then
echo “Monday”
else
if [ $d –eq 3 ]
then
echo “Tuesday”
else
if [ $d –eq 4 ]
then
echo “Wednesday”
else
if [ $d –eq 5 ]
then
echo “Thursday”
else
if [ $d –eq 6 ]
then
echo “Friday”
else
if [ $d –eq 7 ]
then
echo “Saturday”
else
echo “Enter correct day no. between 1-7”
fi
14 NAVIN KUMAR MISHRA
fi
fi
fi
fi
fi
fi

#28. Write a shell script program to print the corresponding day Using elif
structure
echo “Enter day no.”
read d
if [ $d –eq 1 ]
then
echo “Sunday”
elif [ $d –eq 2 ]
then
echo “Monday”
elif [ $d –eq 3 ]
then
echo “Tuesday”
elif [ $d –eq 4 ]
then
echo “Wednesday”
elif [ $d –eq 5 ]
then
echo “Thursday”
elif [ $d –eq 6 ]
then
echo “Friday”
elif [ $d –eq 7 ]
then
echo “Saturday”
else
echo “Enter the day no. between 1-7”
fi
15 NAVIN KUMAR MISHRA
#29. Write a shell script program to print the corresponding day Using case
control structure.

echo “Enter day no.”


read d
case $d in
1)echo “Sunday”
;;
2)echo “Monday”
;;
3)echo “Tuesday”
;;
4)echo “Wednesday”
;;
5)echo “Thursday”
;;
6)echo “Friday”
;;
7)echo “Saturday”
;;
*)echo “Enter the day no. between 1-7”
esac

#30. Write a shell script program to accept a character and check it character
is vowel ,lower alphabet , upper alphabet and digit.

clear
echo “Enter any character”
read d
case $d in
[a,e,i,o,u,A,E,I,O,U])echo “It is a Vowel”
;;
16 NAVIN KUMAR MISHRA
[a-z])echo “It is a Lower case alphabet”
;;
[A-Z)echo “It is a Upper case alphabet”
;;
[0-9])echo “It is a digit”
;;
*)echo “It is a Special Symbol”
esac

#31. Write a shell script program to use control structure.

ch='y'
while [ $ch = 'y' ]
do
echo "Enter u r choice---"
echo "1)No of Users Logged into System"
echo "2)Print Calendar for current year"
echo "3)Print the date"
echo "4)Exit"
read d
case $d in
1)who | wc -l
;;
2)cal 2011
;;
3)date
;;
*)break
esac
echo "Do u wish to Continue (y/n)"
read ch
done

#32. Write a shell script program to check given file is a directory or not
echo “Enter the filename”
read fn
17 NAVIN KUMAR MISHRA
if [ -f $fn ]
then
echo "It is a file "
else
echo "It is a directory "
fi

#33. Write a shell script program to count number of files in a Directory.

echo "No of files : "


k=0
for fi in *
do
k=`expr $k + 1`
done
echo $k

#34. Write a shell script program to search an element is present in list or not

declare -a num[]
echo "Enter length of list"
read n
i=0
echo "Enter list of Numbers"
for ((i=0;i
do
read num[$i]
done
echo "Enter element to be searched"
read x
c=0
for ((i=0;i
18 NAVIN KUMAR MISHRA
do
if [ $x -eq $ { num[$i] } ]
then
c=`expr $c + 1`
fi
done
if [ $c -gt 0 ]
then
echo "It is in the List"
else
echo "It is not in the list"
fi

#35. Write a shell script program to implement read, write, execute


permissions.

echo “enter the name of file”


read fn
if [ -rwx $fn ]
then
echo "it is permitted "
else
echo "not permitted"
fi

#36. Write a shell script program to o copy the contents of one file to
another.

clear
echo "Enter the source file"
read a
echo "Enter the destination file”
read b
cp $a $b
cat $b

19 NAVIN KUMAR MISHRA


20 NAVIN KUMAR MISHRA

You might also like