Shell Scripting : How to Find the Minimum of Three Numbers

Hello again, scripters! In our previous post, we learned how to write a shell script to find the maximum of three numbers. Now, it's time to tackle the flip side of that problem: finding the minimum value.

The logic is almost identical, but we'll swap our comparison operator. This is another fantastic exercise for mastering conditional logic (if/elif/else) in shell scripting.

Let's get right to it!

The Shell Script

Here is the complete, commented script. We'll break down exactly how it works in the next section.

#!/bin/bash

# This is a shell script to find the minimum of three numbers.

# 1. Prompt the user for the numbers
echo "Enter the first number:"
read num1

echo "Enter the second number:"
read num2

echo "Enter the third number:"
read num3

# 2. Compare the numbers to find the minimum
echo "Calculating..."

if [ $num1 -lt $num2 ] && [ $num1 -lt $num3 ]; then
    # -lt means "less than"
    # && means "AND"
    min=$num1
elif [ $num2 -lt $num1 ] && [ $num2 -lt $num3 ]; then
    # elif stands for "else if"
    min=$num2
else
    # If neither num1 nor num2 is the smallest, num3 must be
    min=$num3
fi

# 3. Print the result
echo "The minimum of the three numbers ($num1, $num2, $num3) is: $min"

How It Works: A Step-by-Step Breakdown

As you can see, this script is very similar to our "find_max" script. The key change is one small operator.

  • #!/bin/bash
    The "shebang." This tells your system to use the bash shell to run this script.

  • echo "..." and read num1
    Just like before, we use echo to print a prompt for the user and read to capture their input and store it in a variable (num1, num2, num3).

  • if [ $num1 -lt $num2 ] && [ $num1 -lt $num3 ]; then
    Here is the most important part.
    • -lt: This is the shell's comparison operator for "less than". This is the only major change from our previous script, which used -gt ("greater than").
    • &&: This is the logical "AND" operator.
    • In plain English, this line says: "If the value of num1 is less than num2 AND the value of num1 is also less than num3, then..."

  • min=$num1
    If the "if" statement is true, we create a new variable called min and store the value of num1 in it.

  • elif [ $num2 -lt $num1 ] && [ $num2 -lt $num3 ]; then
    elif is "else if". If the first condition was false, the script checks this one. It says: "Else if the value of num2 is less than num1 AND num2 is also less than num3, then..."

  • min=$num2
    If the "elif" statement is true, we assign the value of num2 to the min variable.

  • else
    This is our "catch-all". If num1 wasn't the minimum, and num2 wasn't the minimum, then by process of elimination, num3 *must* be the minimum.

  • min=$num3
    Assigns the value of num3 to the min variable.

  • fi
    This signals the end of the if/elif/else block (it's "if" backward).

  • echo "The minimum... is: $min"
    Finally, we print a user-friendly message, using the $ symbol to show the values of our variables.

How to Run the Script

The process is the same as before, just with a new filename.

Step 1: Create the file
Open your terminal and use nano (or any text editor) to create your file:

nano find_min.sh

Step 2: Paste the code
Copy the entire shell script from the first section and paste it into the editor.

Step 3: Save and Exit
(In nano: Press Ctrl+O, Enter to save, then Ctrl+X to exit.)

Step 4: Make the script executable
Give your script permission to run:

chmod +x find_min.sh

Step 5: Run it!
Execute your script:

./find_min.sh

Example Output

When you run the script, you should see something like this:

$ ./find_min.sh
Enter the first number:
50
Enter the second number:
100
Enter the third number:
25
Calculating...
The minimum of the three numbers (50, 100, 25) is: 25

Run it again to test another case:

$ ./find_min.sh
Enter the first number:
10
Enter the second number:
15
Enter the third number:
30
Calculating...
The minimum of the three numbers (10, 15, 30) is: 10

Challenge Yourself!

Great job! You've now written scripts to find both the maximum and minimum values. You're getting the hang of conditional logic, variables, and user input.

Want a bigger challenge? Try modifying this script to find the minimum of four numbers. (Hint: You'll need to add another elif statement and adjust your logical && conditions!)

Happy scripting!

Post a Comment

0 Comments