You are on page 1of 11

Ques 3.

Shell programming Shell Script to accept two numbers and perform


all arithmetic operations on it.

Certainly! Here's a simple shell script that accepts two numbers from the user
and performs basic arithmetic operations:

#!/bin/bash

# Function to perform addition


addition() {
echo "Sum: $((num1 + num2))"
}

# Function to perform subtraction


subtraction() {
echo "Difference: $((num1 - num2))"
}

# Function to perform multiplication


multiplication() {
echo "Product: $((num1 * num2))"
}

# Function to perform division


division() {
if [ $num2 -eq 0 ]; then
echo "Error: Division by zero is not allowed."
else
echo "Quotient: $((num1 / num2))"
fi
}

# Function to perform modulus


modulus() {
if [ $num2 -eq 0 ]; then
echo "Error: Modulus by zero is not allowed."
else
echo "Remainder: $((num1 % num2))"
fi
}

# Accept two numbers from the user


read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Perform arithmetic operations


addition
subtraction
multiplication
division
modulus
Save this script to a file, for example, arithmetic_operations.sh. Make the script
executable using the following command:

chmod +x arithmetic_operations.sh

Then, run the script:


./arithmetic_operations.sh

The script will prompt you to enter two numbers, and it will perform addition,
subtraction, multiplication, division, and modulus operations on them,
displaying the results accordingly.

• Menu Based Shell Script.

Certainly! Here's another example of a menu-based shell


script. This script will provide options for basic file operations:

while true; do
# Display menu
echo "Menu:"
echo "1. List files in the current directory"
echo "2. Display the current date and time"
echo "3. Print the working directory"
echo "4. Exit"
# Accept choice from the user
read -p "Enter your choice (1-4): " choice

# Check the user's choice


case $choice in
1)
echo "Files in the current directory:"
ls
;;
2)
echo "Current date and time:"
date
;;
3)
echo "Current working directory:"
pwd
;;
4)
echo "Exiting the script."
exit 0
;;
*)
echo "Invalid choice. Please enter a number between 1 and 4."
;;
esac

# Prompt the user to continue or exit


read -p "Do you want to continue? (y/n): " continue_choice

case $continue_choice in
[Nn]*)
echo "Exiting the script."
exit 0
;;
esac
done

This script allows the user to choose between listing files in the
current directory, displaying the current date and time, printing the
working directory, or exiting the script. After each operation, the user
is prompted to continue or exit.

• Shell Script to calculate the Gross Salary


#!/bin/bash

# Function to calculate gross salary


calculate_gross_salary() {
basic_salary=$1
allowances=$2
deductions=$3

# Calculate gross salary


gross_salary=$((basic_salary + allowances - deductions))

echo "Gross Salary: $gross_salary"


}

# Accept input from the user


read -p "Enter Basic Salary: " basic_salary
read -p "Enter Allowances: " allowances
read -p "Enter Deductions: " deductions

# Call the function to calculate gross salary


calculate_gross_salary $basic_salary $allowances
$deductions

The script will prompt you to enter the basic salary,


allowances, and deductions. It will then calculate and display
the gross salary based on the provided values.
Feel free to customize the script based on your specific salary
calculation requirements. Adjust the formula or add more
input parameters as needed.

• Shell Script through case statement

Certainly! Here's an example of a shell script that uses a case


statement to calculate the gross salary based on different
salary structures:

#!/bin/bash

# Function to calculate gross salary


calculate_gross_salary() {
basic_salary=$1
allowances=$2
deductions=$3

# Calculate gross salary


gross_salary=$((basic_salary + allowances - deductions))
echo "Gross Salary: $gross_salary"
}

# Display menu
echo "Salary Calculation Menu:"
echo "1. Manager"
echo "2. Engineer"
echo "3. Intern"
echo "4. Exit"

# Accept choice from the user


read -p "Enter your choice (1-4): " choice

# Check the user's choice using a case statement


case $choice in
1)
# Manager salary structure
calculate_gross_salary 50000 20000 10000
;;
2)
# Engineer salary structure
calculate_gross_salary 40000 15000 8000
;;
3)
# Intern salary structure
calculate_gross_salary 20000 5000 2000
;;
4)
echo "Exiting the script."
exit 0
;;
*)
echo "Invalid choice. Please enter a number between 1
and 4."
;;
Esac

The script presents a menu with different job roles (Manager,


Engineer, Intern). Based on the user's choice, it calculates and
displays the gross salary using the calculate_gross_salary
function. The user can exit the script by selecting option 4.
Adjust the salary structures in the case statement as needed
for your specific
• Shell Script to find the largest among numbers
using positional parameters

#!/bin/bash

# Function to find the largest number among positional parameters


find_largest() {
largest=$1

# Iterate through positional parameters


for num in "$@"; do
if [ "$num" -gt "$largest" ]; then
largest=$num
fi
done

echo "The largest number is: $largest"


}

# Check if at least two positional parameters are provided


if [ "$#" -lt 2 ]; then
echo "Please provide at least two numbers as positional
parameters."
else
# Call the function to find the largest number
find_largest "$@"
fi

This script defines a function find_largest that takes positional


parameters and finds the largest among them. The main part of the
script checks if at least two positional parameters are provided and
calls the function accordingly. Adjust the script as needed based on
your specific requirements.

You might also like