0% found this document useful (0 votes)
8 views4 pages

OS Lab Exp 2

The document contains a series of Bash scripts that perform various tasks, including calculating factorials, generating multiplication tables, reversing numbers, listing files, determining the day of the week based on user input, and performing basic arithmetic operations. Each example is structured to prompt the user for input and execute the corresponding operation. The scripts demonstrate fundamental programming concepts such as loops, conditionals, and user interaction.

Uploaded by

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

OS Lab Exp 2

The document contains a series of Bash scripts that perform various tasks, including calculating factorials, generating multiplication tables, reversing numbers, listing files, determining the day of the week based on user input, and performing basic arithmetic operations. Each example is structured to prompt the user for input and execute the corresponding operation. The scripts demonstrate fundamental programming concepts such as loops, conditionals, and user interaction.

Uploaded by

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

EXP – 2

EX – 1

echo -n "Enter a number: "

read num
factorial=1
for ((i=1; i<=num; i++)); do
factorial=$((factorial * i))
done

echo "Factorial of $num is $factorial"

EX – 2

#!/bin/bash

echo -n "Enter a number: "


read num
for ((i=1; i<=10; i++)); do
echo "$num x $i = $((num * i))"
done

EX – 3

#!/bin/bash
echo -n "Enter a number: "

read num
reverse=0
while [ $num -gt 0 ]; do
remainder=$((num % 10))
reverse=$((reverse * 10 + remainder))

num=$((num / 10))
done
echo "Reversed number: $reverse"

EX – 4

#!/bin/bash
for file in *; do
echo "$file"
done

EX – 5

#!/bin/bash
echo -n "Enter a number (1-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

EX – 6

#!/bin/bash
echo "Select an operation: "

echo "1. Addition"


echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read -p "Enter choice [1-4]: " choice

case $choice in
1) read -p "Enter two numbers: " a b
echo "Result: $((a + b))" ;;
2) read -p "Enter two numbers: " a b
echo "Result: $((a - b))" ;;

3) read -p "Enter two numbers: " a b


echo "Result: $((a * b))" ;;
4) read -p "Enter two numbers: " a b
if [ $b -eq 0 ]; then
echo "Division by zero is not allowed"

else
echo "Result: $((a / b))"
fi ;;
*) echo "Invalid choice" ;;
esac

You might also like