0% found this document useful (0 votes)
104 views9 pages

Notes: Sample Source Codes For Programming in Shell For Unix

The document provides 17 questions for a Linux final exam lab program. The questions cover shell scripting concepts like generating Fibonacci series, factorials, checking Armstrong numbers, building an arithmetic calculator, multiplication tables, converting between binary and decimal, checking for palindromes, displaying system information, variable swapping, reading command line arguments, checking file permissions, and calculating areas of shapes. Students are asked to write shell scripts to implement the given tasks.

Uploaded by

damner
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
104 views9 pages

Notes: Sample Source Codes For Programming in Shell For Unix

The document provides 17 questions for a Linux final exam lab program. The questions cover shell scripting concepts like generating Fibonacci series, factorials, checking Armstrong numbers, building an arithmetic calculator, multiplication tables, converting between binary and decimal, checking for palindromes, displaying system information, variable swapping, reading command line arguments, checking file permissions, and calculating areas of shapes. Students are asked to write shell scripts to implement the given tasks.

Uploaded by

damner
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

LINUX FINAL LAB PROGRAMS FOR END EXAM (March -22/03/2021)

Course Teacher: Prof. Tanaji Patil


Q1. Write a shell script for Fibonacci series generation
#/bin/bash
echo "program to find fibonacci series"
echo "enter number"
read n
x=0
y=1
i=2
echo "fibonacci series up to $n"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1`
z=`expr $x + $y`
echo "$z"
x=$y
y=$z
done

Q2. Write a shell script for factorial of a given number


#!/bin/bash
read -p "Enter a number" num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact*num))
num=$((num-1))
done

echo $fact
Q3. Write a shell script for checking Armstrong number or not
#!/bin/bash
echo "Enter Number"
read c

x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done

if [ $sum -eq $c ]
then
echo "It is an Armstrong Number"
else
echo "It is not Armstrong Number"
fi

Q4. Write a shell script to design an arithmetic calculator


#!/bin/bash
# take user input
echo "enter two number :"
read a
read b
# input type of operations
echo "enter choice :"
echo "1.addition"
echo "2.subtration"
echo "3.multiplication"
echo "4.division"
read ch
# switch case to perform
# calculater operations
case $ch in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a - $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo "scale=2; $a / $b" | bc`
;;
Q5. Write a shell script for generation of multiplication table.
#!/bin/bash
clear
echo "which number to generate multiplication table"
read number
i=1
while [ $i -le 10 ]
do

echo " $number * $i = `expr $number \* $i`"


i=`expr $i + 1`
done

Q6. Write a shell script to convert binary to decimal


#!/bin/bash
echo "Enter any Binary no:"
read bino
len=${#bino}
i=1
pow=$((len - 1 ))
while [ $i -le $len ]
do
n=`echo $bino | awk '{ printf substr($0,'$i',1)}'`
j=1
p=1
while [ $j -le $pow ]

do
p=$(( p * 2 ))
j=(( j + 1 ))
done
dec=$(( n * p ))
findec=$(( findec + dec ))
pow=$(( pow - 1 ))

i=$(( i + 1 ))
done
echo "Equivalent Decimal no:" $findec
Q7. Write a shell script to check for palindrome of a number.
#!/bin/bash

echo "enter n"


read n
num=0
on=$n
while [ $n -gt 0 ]
do
num=$(expr $num \* 10)
k=$(expr $n % 10)
num=$(expr $num + $k)
n=$(expr $n / 10)
done
if [ $num -eq $on ]
then

echo "It’s a palindrome number"


else
echo "It’s not a palindrome number"
fi

Q8. Write a shell script for finding the information about login name and file
name
#!/bin/bash
echo "Your user name: $(echo $USER)"

echo "Current date and time : $(date)"

echo "currently logged on users:"


who

echo "files related to user:"


ls
Q9. Write a shell script to exchange the contents of two variables

#!/bin/bash
echo "Enter value of x:"
read x
echo "Enter value of y:"
read y
echo "Before swap, x=$x and y=$y"
z=$x
x=$z
y=$z
echo "after swap, x=$x and y=$y"

Q10. Write a shell script to read integer number from 1 to 20


#!/bin/bash
clear
n=1
while [ $n -le 20 ]
do
echo $n
n=`expr $n + 1`
done

Q11. Write a shell script to count the number of lines in a file


#!/bin/bash
echo "Enter the filename"
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo "Number of characters in $file is:" $c
echo "Number of words in $file is:" $w
echo "Number of lines in $file is:" $l
Q12. Write a shell script to display parent PID and child PID
#include <stdio.h>
#include <unistd.h>

int main()
{
int p_id,p_pid;
p_id=getpid();
p_pid=getppid();
printf("Child Proce id is: %d\n", p_id);
printf("Parent Process id is: %d\n", p_pid);
return 0;
}

Q13. Write a shell script to accept three command line arguments and display
each of them.
#!/bin/bash
echo "Script Name: $0"
echo "First Parameter of the script is $1"
echo "The Second Parameter is $2"
echo "The Complete list of arguments is $@"
echo "Total Number of Parameters: $#"
echo "The process ID is $$"
echo "Exit code for the script:$?"
Q14. Write a shell script to check whether the given file is present in a directory
and what permission are given to the owner.

#!/bin/bash
echo "Enter the file name"
read fname
if test -f $fname
the
echo "it is a file it has"
if test -r $fname
then
echo "read permission"
fi
if test -w $fname
the
echo "write permission"
fi
if test -x $fname
then
echo "execute permission"
else
echo "the given file does not exist" $fname
fi

Q15. Write a shell script to find area of circle, square, triangle and rectangle.
#!/bin/bash
echo -n "Enter base of traingle"
read b
echo -n "Enter height of a traingle"
read h
area=$(echo "scale=2;(1/2)*$b*$h" | bc)
echo "area of a traingle is $area"
echo -n " Enter the radius of the circle"
read r
area=$(echo "scale=2;3.14*($r*$r)" | bc)
echo "area of circle is $area"
echo "Enter a side of the square"
read s
echo "area of a square :`expr $s \* $s`"
echo "enter the length and breadth of a rectangle"
read leng
read brea
echo "area of the reactangle is :`expr $leng \* $brea`"
Q16. Write a shell script to accept three subject marks scored by a student and
declare result.

#!/bin/bash
tput clear
echo "Enter Student Name:"
read name
echo "Enter roll number:"
read rollnumber
echo "Enter 3 Subject marks:"
read m1 m2 m3
echo "Name of the student is: $name"
echo "Student Roll Number is: $rollnumber"
echo "3 Subject Marks"
echo "m1 m2 m3"
echo $m1 $m2 $m3

per=`echo \($m1 + $m2 + $m3\) /3|bc`


echo "Percentage is:" $per
if test $per -ge 60
then
echo "Grade:First Class"
elif test $per -ge 50 -a $per -le 59
then
echo "Grade:Second Class"
elif test $per -ge 40 -a $per -le 49
then
echo "Grade:Third Class"
else
echo "Fail"
fi
Q17. Write a shell script to perform arithmetic operation on two number
depending on +, - , * , /

#!/bin/bash
clear
sum=0
i="y"
echo "enter first number:"
read n1
echo "enter second number:"
read n2
while [ $i = "y" ]
do

echo "1.addition"
echo "2.subtration"
echo "3.multiplication"
echo "4.division"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "sum = " $sum ;;

2)sum=`expr $n1 - $n2`


echo "sub = " $sum ;;

3)sum=`expr $n1 \* $n2`


echo "mul = " $sum ;;

4)sum=`expr $n1 / $n2`


echo "div = " $sum ;;
*) echo "invalid choice" ;;
esac
echo "fo u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done

You might also like