0% found this document useful (0 votes)
71 views

Unix Lab Assignment

Uploaded by

Nisha Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Unix Lab Assignment

Uploaded by

Nisha Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

INDEX

SL Assignments Teacher’s Signature


No.
1 Write a shell script to obtain a list of
prime numbers in a range given by
the user.
2 Write a shell script to print all odd
numbers in a range given by the user.
3 Write a shell script to print the
following series... 1 1 1 3 5 9 17 ... ...
n
4 Write a shell script to convert a
decimal number into binary.
5 Write a shell script to convert a binary
number into octal.
6 Write a shell script to sort n numbers
using bubble sort algorithm.
7 Write a shell script to find an item
from a list using binary search.
8 Write a shell script to add all the
digits of a number.
9 Write a shell script to write a four
digit number in reverse order.
10 Write a shell script to create the
following triangle.
1
121
12321
1234321
11 Write a shell script to create the
floyd’s triangle.
12 Write a shell script to find the product
of first n natural numbers.
13 Write a shell script to find the sum of
cubes of first n natural numbers.
14 Write a shell script to find the
factorial of a number.
15 Write a shell script to reverse a string.
16 Write a shell script to find out if a
word is palindrome or not.
17 Write a shell script to write a name
written in following format as...
‘RAHUL KUMAR
SHARMA’ into
‘R. K. SHARMA’
18 Write a shell script to check if a
number is Armstrong or not.
19 Write a shell script to print the name
of the day of a month if date is
entered.
Assignment 1:
Write a shell script to obtain a list of prime numbers in a range given by the user.

Source Code :
echo "Enter the lower bound : "
read lb
echo "Enter the upper bound : "
read ub
if [ $lb -le 0 ]
then
lb=1
fi
for((i=lb;i<=ub;i++))
do
l=`echo "scale=0;sqrt($i)"|bc`
for((j=3;j<=l;j++))
do
if [ $((i%j)) -eq 0 ]
then
break
fi
done
if [ $j -gt $l ]
then
echo -n $i" "
fi
done

Output 1:
Enter the lower bound :
-3
Enter the upper bound :
20
1 2 3 4 5 6 7 8 10 11 13 14 17 19
Output 2:
Enter the lower bound :
10
Enter the upper bound :
30
10 11 13 14 17 19 22 23 26 29
Assignment 2:
Write a shell script to print all odd numbers in a range given by the user.

Source Code :
echo "Enter the lower bound : "
read lb
echo "Enter the upper bound : "
read ub
if [ $lb -le 0 ]
then
lb=1
elif [ $((lb%2)) -eq 0 ]
then
let lb=$lb+1
fi
for((i=lb;i<=ub;i=i+2))
do
echo -n $i" "
done

Output 1:
Enter the lower bound :
0
Enter the upper bound :
20
1 3 5 7 9 11 13 15 17 19
Output 2:
Enter the lower bound :
5
Enter the upper bound :
25
5 7 9 11 13 15 17 19 21 23 25
Assignment 3:
Write a shell script to print the following series...
1 1 1 3 5 9 17 ... ... n
Source Code :
a=1
b=1
c=1
echo "Enter the number of terms:"
read n if [ $n -eq 1 ]
then
echo $a" "
elif [ $n -eq 2 ]
then
echo $a" "$b" "
elif [ $n -eq 3 ]
then
echo $a" "$b" "$c" "
else
echo -n $a" "$b" "$c" "
for((i=4;i<=n;i++))
do
sum=`expr $a + $b + $c`
echo -n $sum" "
a=$b
b=$c
c=$sum
done
fi
Output 1:
Enter the number of terms :
8
1 1 1 3 5 9 17 31
Output 2:
Enter the number of terms :
2
11
Assignment 4:
Write a shell script to convert a decimal number into binary.

Source Code :
echo -n "Enter number in decimal : "
read n
d=`echo "ibase=10;$n"|bc`
d=`echo "obase=2;$d"|bc` echo
"The result in binary : $d"

Output 1:

Enter number in decimal :


20
The result in binary : 10100

Output 2:

Enter number in decimal :


10
The result in binary : 1010
Assignment 5:
Write a shell script to convert a binary number into octal.

Source Code :
echo -n "Enter number in binary : "
read n
d=`echo "ibase=2;$n"|bc`
d=`echo "obase=8;$d"|bc`
echo "The result in octal : $d"

Output 1:
Enter number in binary :
10110
The result in octal : 26

Output 2:

Enter number in binary :


1010
The result in octal : 12
Assignment 6:
Write a shell script to sort n numbers using bubble sort algorithm.

Source Code :
echo -n "Enter the no. of elements:"
read n
echo
for((i=0;i<n;i++))
do
echo -n "Enter value : "
read a[$i]
done
for((i=0;i<n;i++))
do
for((j=0;j<n-i-1;j++))
do
if [ ${a[$j]} -gt ${a[$j+1]}
] then
m=${a[$j]}
a[$j]=${a[$j+1]}
a[$j+1]=$m
fi
done
done
echo
echo -n "sorted elements are : "
for((i=0;i<n;i++))
do
echo -n ${a[$i]}" "
done
Output 1:
Enter the no. of elements : 6

Enter value : 9
Enter value : 4
Enter value : 8
Enter value : 7
Enter value : 2
Enter value : 6

sorted elements are : 2 4 6 7 8 9


Assignment 7:
Write a shell script to find an item from a list using binary search.

Source Code :
lower=0
flag=1
echo -n "Enter the number to search : "
read num
for((mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2))
do
if [ ${a[$mid]} -eq $num ]
then
echo "Number is preasent at position `expr $mid + 1` in the
array"
flag=0
break
fi
if [ ${a[$mid]} -gt $num ]
then
upper=`expr $mid - 1`
else
lower=`expr $mid + 1`
fi
done
if [ $flag -eq 1 ]
then
echo "Number is not preasent in the array"
fi

Output 1:
How many elements do you want to enter?
5
Enter the numbers into the array:
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4
a[5] = 5
Enter the number to search : 3
Number is preasent at position 3 in the array
Assignment 8:
Write a shell script to add all the digits of a number.

Source Code :
echo "Enter the number : "
read n
sum=0
i=1
x=`echo $n|cut -c $i`
while [ "$x" != "" ]
do
sum=`expr $sum + $x`
i=`expr $i + 1`
x=`echo $n|cut -c $i`
done
echo $sum

Output 1:
Enter the number :
1234
10

Output 2:
Enter the number :
159357
30
Assignment 9:
Write a shell script to write a four digit number in reverse order.

Source Code :
echo "enter the number:"
read n
i=$n x=0
sum=0
while [ $n -gt 0 ]
do
x=`expr $n % 10`
sum=`expr $sum \* 10`
sum=`expr $sum + $x`
n=`expr $n / 10`
done
echo "Result is : $sum"

Output 1:
enter the number :
1234
Result is : 4321

Output 2:
enter the number :
9546321
Result is : 1236459
Assignment 10:
Write a shell script to create the following triangle.
1
121
12321
1234321

Source Code :
echo "Enter the line number : "
read n
for((l=1;l<=n;l++))
do
for((i=1;i<=n-l;i++))
do
echo -n " "
done
for((i=1;i<=l;i++))
do
echo -n " $i "
done
for((i=l-1;i>0;i--))
do
echo -n " $i "
done
echo
done
Output 1:
Enter the line number :
4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Assignment 11:
Write a shell script to create the floyd’s triangle.

Source Code :
echo -n "Enter the number of lines : "
read l
for((i=1,k=1;i<=l;i++))
do
for((j=1;j<=i;j++))
do
echo -n "$k "
let k=$k+1
done
echo
done
Output 1:
Enter the number of lines : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Assignment 12:
Write a shell script to find the product of first n natural numbers.

Source Code :
echo -n "Enter the number of elements:"
read n
result=1
for((i=1;i<=n;i++))
do
result=`expr $result \* $i`
done
echo "Product of first $n natural number is : $result"
Output 1:
Enter the number of elements :
5
Product of first 5 natural number is : 120
Output 2:

Enter the number of elements :


6
Product of first 5 natural number is : 720
Assignment 13:
Write a shell script to find the sum of cubes of first n natural numbers.

Source Code :
echo -n "Enter the number of elements : "
read n
sum=0
y=3
for((i=1;i<=n;i++))
do
s=`echo "(($i^$y))"|bc -l`
sum=`expr $sum + $s`
done
echo "Sum of cubes of first $n natural number is : $sum"
Output 1:

Enter the number of elements : 5


Sum of cubes of first 5 natural number is : 225
Output 2:
Enter the number of elements : 6
Sum of cubes of first 6 natural number is : 441
Assignment 14:
Write a shell script to find the factorial of a number.

Source Code :
echo "Enter the number : "
read n
f=1
while [ $n -ne 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "factorial : "$f

Output 1:
Enter the number :
4
factorial : 24

Output 2:
Enter the number :
0
factorial : 1

Output 3:
Enter the number :
1
factorial : 1

Output 4:
Enter the number :
6
factorial : 720
Assignment 15:
Write a shell script to reverse a string.

Source Code :

echo "Enter the string"


read str
fstr=""
i=1
ch=`echo $str | cut -c $i`
while [ "$ch" != "" ]
do
fstr=$ch$fstr
i=`expr $i + 1`
ch=`echo $str | cut -c $i`
done
echo "Output : "$fstr
Output 1:
Enter the string
swarnav
Output : vanraws
Output 2:

Enter the string


abcdefgh
Output : hgfedcba
Assignment 16:
Write a shell script to find out if a word is palindrome or not.

Source Code :

echo "Enter the string"


read str
fstr=""
i=1
ch=`echo $str | cut -c $i`
while [ "$ch" != "" ]
do
fstr=$ch$fstr
i=`expr $i + 1`
ch=`echo $str | cut -c $i`
done
if [ "$str" = "$fstr" ]
then
echo "$str is palindrome"
else
echo "$str is not palindrome"
fi

Output 1:

Enter the string Parwez


Parwez is not palindrome

Output 2:

Enter the string


madam
madam is palindrome
Assignment 17:
Write a shell script to write a name written in following format as...
‘RAHUL KUMAR SHARMA’
into
‘R. K. SHARMA’

Source Code :
echo -n "Enter the string : "
read str
line=`echo $str|tr -s''`
IFS=' ' set $line
fstr="" j=1
for i in $*
do
ch=`echo $i|cut -c 1`
if [ $j -eq $# ]
then
ch=`echo $i|cut -f $j`
fstr=$fstr$ch
break
fi
fstr=$fstr$ch".
"
let i=$i+1
let j=$j+1
done
echo $fstr
Output 1:
Enter the string : RAHUL KUMAR SHARMA
R. K. SHARMA
Assignment 18:
Write a shell script to check if a number is Armstrong or not.

Source Code :
echo -n "Enter the number : "
read n
m=$n
c=0
while [ $n -ne 0 ]
do
n=`expr $n / 10`
c=`expr $c + 1`
done
n=$m
sum=0
while [ $m -ne 0 ]
do
x=`expr $m % 10`
p=1
for((i=1;i<=c;i++))
do
p=`expr $p \* $x`
done
sum=`expr $sum + $p`
m=`expr $m / 10`
done
if [ $sum -eq $n ]
then
echo "$sum is Armstrong number"
else
echo "$sum is not Armstrong number"
fi

Output 1:

Enter the number : 153


153 is Armstrong number
Output 2:

Enter the number : 142


73 is not Armstrong number
Assignment 19:
Write a shell script to print the name of the day of a month if date is entered.

Source Code :
echo "Enter the date(dd/mm/yyyy):"
read d read m
read y date +%A -
d$y$m$d

Output 1:
Enter the date(dd/mm/yyyy) :
12
12
2019
Thursday
Output 2:
Enter the date(dd/mm/yyyy) :
21
12
2019
Saturday
Output 3:
Enter the date(dd/mm/yyyy) :
01
05
2012
Tuesday
Output 4:
Enter the date(dd/mm/yyyy) :
10
01
2010
Sunday

You might also like