Largest of three Numbers in Shell (SH)

When writing a shell script to find the largest of three numbers, the process involves reading input from the user, comparing the numbers, and determining which one is the largest. Shell scripts are powerful tools in Unix/Linux environments, and they help automate repetitive tasks. In this script, we will use basic shell scripting constructs such as variable assignment, conditional statements, and arithmetic operations to achieve our goal.

In this shell script, we compare three numbers—num1, num2, and num3—to determine the largest among them. This is achieved using conditional statements. The logic involves sequentially comparing each pair of numbers. If num1 is greater than or equal to both num2 and num3, then num1 is the largest. If not, the script checks if num2 is greater than or equal to both num1 and num3. If this condition holds true, then num2 is the largest. If neither of these conditions is true, the script concludes that num3 must be the largest. This systematic comparison ensures that all possible scenarios are covered, guaranteeing the correct result.

Prompt for Input:

  • First, we need to prompt the user to enter three numbers. This can be achieved using the echo command to display a message and the read command to capture the user’s input.

Variable Assignment:

  • Assign the user input to three variables, say num1, num2, and num3.

Comparison Logic:

  • Use conditional statements (if-elif-else) to compare the three numbers.
  • For the comparisons, utilize arithmetic comparison operators within the double parentheses (( )) for more intuitive arithmetic operations.
  • Determine which number is the largest by comparing each pair of numbers and updating a variable, say largest, with the maximum value.

Output the Result:

  • Finally, print the largest number using the echo command.

Shell Script

Here is the complete shell script implementing the above logic:

#!/bin/bash

# Prompt the user to enter three numbers
echo "Enter first number: "
read num1
echo "Enter second number: "
read num2
echo "Enter third number: "
read num3

# Compare the numbers and find the largest using (( ))
if (( num1 >= num2 && num1 >= num3 )); then
    largest=$num1
elif (( num2 >= num1 && num2 >= num3 )); then
    largest=$num2
else
    largest=$num3
fi

# Print the largest number
echo "The largest number is: $largest"

Explanation

Prompt for Input:

echo "Enter first number: "
read num1
echo "Enter second number: "
read num2
echo "Enter third number: "
read num3

The script begins by prompting the user to enter three numbers. The echo command displays the prompt and the read command captures the input and assigns it to the variables num1, num2, and num3.

Comparison Logic:

if (( num1 >= num2 && num1 >= num3 )); then
    largest=$num1
elif (( num2 >= num1 && num2 >= num3 )); then
    largest=$num2
else
    largest=$num3
fi
  • The script then uses an if-elif-else structure to compare the three numbers.
  • if (( num1 >= num2 && num1 >= num3 )): Checks if num1 is greater than or equal to both num2 and num3. If true, num1 is assigned to the variable largest.
  • elif (( num2 >= num1 && num2 >= num3 )): If the first condition is false, this checks if num2 is greater than or equal to both num1 and num3. If true, num2 is assigned to largest.
  • else: If neither of the above conditions is true, num3 is assigned to largest.

Output the Result:

echo "The largest number is: $largest"
  • Finally, the script prints the largest number using the echo command.

This script demonstrates the basic structure and flow of a shell script used to find the largest of three numbers. The use of (( )) for arithmetic comparisons makes the logic clear and easy to understand.

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.

Add a Comment

Your email address will not be published. Required fields are marked *