CSE-325 Unit-2 (Shell Scripting) Lab Exercises

 Exercise 1: Write a shell script that calculates the factorial of a user-provided number using a for loop.

#!/bin/bash

echo "Enter a number:"

read num

factorial=1

for (( i=1; i<=num; i++ ))

do

  factorial=$(( factorial * i ))

done

echo "Factorial of $num is $factorial"


  Exercise 2: Develop a seript that prints the multiplication table for a given number up to 10 using a for loop.

#!/bin/bash

echo "Enter a number to print its multiplication table:"

read num

for (( i=1; i<=10; i++ ))

do

  echo "$num x $i = $(( num * i ))"

done

 Exercise 3: Create a wript that reverses a given sumber using a while loop. 

#!/bin/bash

echo "Enter a number to reverse:"

read num

reverse=0

while [ $num -gt 0 ]

do

  remainder=$(( num % 10 ))

  reverse=$(( reverse * 10 + remainder ))

  num=$(( num / 10 ))

done

echo "Reversed number is $reverse"

Exercise 4: Wire a script to display all files in a directory usmg a for loop.

#!/bin/bash

echo "Enter directory path:"

read dir

if [ -d "$dir" ]; then

  for file in "$dir"/*

  do

    echo "$file"

  done

else

  echo "Directory does not exist."

fi


Exercise 5 Write a shell script named day.of.week.sh that prompts the user to enter a number between 1 and 7. representing a day of the week. 
Use a case structure to print the corresponding day of the week:
 If the user enters 1. print "Sunday "
If the user enters 2. prut "Monday "
 If the user enters 3. print Tuesday "
If the user enters 4. print Wednesday " 
 If the user enters 5. print Thursday " 
If the user enters 6. print "Friday " 
 If the user enters 7. print "Saturday"
 If the user enters a number outside the range, print "Invalid Input. Please enter a number between 1 and 7".

#!/bin/bash
echo "Enter a number between 1 and 7:"
read day
case $day in
  1) echo "Sunday" ;;
  2) echo "Monday" ;;
  3) echo "Tuesday" ;;
  4) echo "Wednesday" ;;
  5) echo "Thursday" ;;
  6) echo "Friday" ;;
  7) echo "Saturday" ;;
  *) echo "Invalid input. Please enter a number between 1 and 7." ;;
esac


 Exercise 6. Create a nesu-driven vakulatus wript that performs humic arithmetic operatiou Land on er selestion using a cаде четат


#!/bin/bash

echo "Select an arithmetic operation:"

echo "1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

read choice

echo "Enter first number:"

read num1

echo "Enter second number:"

read num2

case $choice in

      1) result=$((num1 + num2))

             echo "Result: $num1 + $num2 = $result" ;;

      2) result=$((num1 - num2))

             echo "Result: $num1 - $num2 = $result" ;;

      3) result=$((num1 * num2))

         echo "Result: $num1 * $num2 = $result" ;;

      4) if [ $num2 -ne 0 ]; then

                  result=$((num1 / num2))

                  echo "Result: $num1 / $num2 = $result"

         else

                  echo "Division by zero is not allowed."

         fi ;;

      *) echo "Invalid choice." ;;

esac





Post a Comment

Previous Post Next Post