You are on page 1of 9

OS Assignment-02

ANAY GUPTA | 2005780


Q1. Write a shell program to take input of your name from the command line and display the same.

#!/usr/bin/bash
echo "Your name is $1."

Q2. Write a shell program that multiply two numbers using the command line arguments.

#!/usr/bin/bash
p=$(expr "$1" \* "$2")
echo "Product = $p"

Q3. Write a shell program to check whether a given number is even or odd.

#!/usr/bin/bash
read -p "Enter a number: " num
if
[ `expr $num % 2` -eq 0 ]
then
echo "$num is an even number."
else
echo "$num is a odd number."
fi
Q4. Write a shell program to find greatest and smallest among three given numbers using nested if.

#!/usr/bin/sh
read -p "Enter first number: " num1
read -p "Enter secound number: " num2
read -p "Enter third number: " num3
g=$num1
s=$num1
if [ $num1 -gt $num2 ]; then
if [ $num1 -gt $num3 ]
then
echo "Greatest = $num1"
else
echo "Greatest = $num3"
fi
else
if [ $num2 -gt $num3 ]
then
echo "Greatest = $num2"
else
echo "Greatest = $num3"
fi
fi
if [ $num1 -lt $num2 ]; then
if [ $num1 -lt $num3 ]
then
echo "Smallest = $num1"
else
echo "Smallest = $num3"
fi
else
if [ $num2 -lt $num3 ]
then
echo "Smallest = $num2"
else
echo "Smallest = $num3"
fi
fi
Q5. Write a shell program to enter the mark secured by a student in a particular subject. Based on
the grading system of our university, find and display the grade (i.e. from “O” to “F”) secured by the
student in that subject.

#!/usr/bin/bash
read -p "Enter your marks out of 100: " m
if [ $m -le 100 ] && [ $m -gt 90 ]; then
g="O"
elif [ $m -le 90 ] && [ $m -gt 80 ]; then
g="E"
elif [ $m -le 80 ] && [ $m -gt 70 ]; then
g="A"
elif [ $m -le 70 ] && [ $m -gt 60 ]; then
g="B"
elif [ $m -le 60 ] && [ $m -gt 50 ]; then
g="C"
elif [ $m -le 50 ] && [ $m -gt 40 ]; then
g="D"
elif [ $m -le 40 ] && [ $m -gt 0 ]; then
g="F"
else
g="Invalid input"
fi
echo "Grade = $g"
Q6. Write a menu-based shell program using switch case statement, that will carry out the following
file management operations based on the specified choice value. 1 - List files, 2 - Create directory, 3
– Change directory, 4 – Remove directory, 5 - Create file, 6 – Copy file, 7 – remove file.

#!/usr/bin/bash
echo "Enter which menu you wanna select."
echo "1 - List files"
echo "2 - Create Directory"
echo "3 - Change Directory"
echo "4 - Remove Directory"
echo "5 - Create File"
echo "6 - Copy File"
echo "7 - Remove File"
read op
case $op in
1)ls
;;

2)read -p "Enter the name of the directory you wanna create: " temp
if [ ! -d "$temp" ]; then
mkdir $temp;
echo "Created successfully"
else
echo "Directory already exists"
fi
;;

3)read -p "Enter the path where you wanna move: " temp
cd $temp
;;

4)read -p "Enter the name of the directory you wanna remove: " temp
if [ -d "$temp" ]; then
rm -rf "$temp"
echo "Removed successfully"
else
echo "Directory doesn't exists"
fi
;;

5)read -p "Enter the name of the file (without .txt): " temp
if [ -e "$temp" ]; then
echo "File already exists"
else
echo "enter text here" >> "$temp".txt
fi
;;

6)read -p "Please provide a path to the file: " srcpath


read -p "Please provide a path to the file: " dstpath
if [ ! -f "$srcpath" ]; then
echo "Source path: $srcpath doesn't exist"
else
mkdir -p "$dstpath"
cp -r "$srcpath" "$dstpath"
echo "Copied successfully"
fi
;;

7)read -p "Please provide a path to the file: " path


if [ ! -f "$path" ]; then
echo "Source path: $path doesn't exist"
else
rm "$path"
echo "removed successfully"
fi
;;

*)echo "invalid option"


;;
esac
Q7. Write a shell program to find whether a given year is a leap year or not.

#!/usr/bin/bash
read -p "Enter year: " year
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
echo "$year is a leap year."
elif [ `expr $year % 100` -eq 0 ]; then
echo "$year is not a leap year."
else
echo "$year is a leap year."
fi
else
echo "$year is not a leap year."
fi
Q8. Write a shell program to display the prime numbers between 1 and 100.

#!/usr/bin/bash
for i in {2..100}
do
f=0
j=2
while [ $j -le `expr $i / 2` ]
do
if [ `expr $i % $j` -eq 0 ]; then
f=1
break
fi
j=`expr $j + 1`
done
if [ $f -eq 0 ]; then
echo "$i"
fi
done
Q9. Write a shell script to verify whether the given string is a palindrome or not.
#!/usr/bin/bash
read -p "Enter a string without spaces: " vstr
i=0
for i in $(seq 0 ${#vstr})
do
rvstr=${vstr:$i:1}${rvstr}
done
if [ "$vstr" = "$rvstr" ]; then
echo "$vstr is a palidrome."
else
echo "$vstr is not a palindrome."
fi

You might also like