Hey there, fellow coders and scripters! Welcome to a quick tutorial on shell scripting. Shell scripts are a powerful way to automate tasks on your Linux or macOS system, and one of the best ways to learn is by solving simple, practical problems.
Today, we're going to tackle a classic beginner exercise: writing a shell script that asks the user for three numbers and then determines which one is the maximum.
Let's dive in!
The Shell Script
First, 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 maximum 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 maximum
echo "Calculating..."
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
# -gt means "greater than"
# && means "AND"
max=$num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
# elif stands for "else if"
max=$num2
else
# If neither num1 nor num2 is the greatest, num3 must be
max=$num3
fi
# 3. Print the result
echo "The maximum of the three numbers ($num1, $num2, $num3) is: $max"
How It Works: A Step-by-Step Breakdown
Let's break that script down line by line so you understand every part.
#!/bin/bash
This is called a "shebang." It's always the very first line of a script, and it tells the operating system which interpreter to use to run the code. In this case, we're usingbash, the most common shell.echo "Enter the first number:"
Theechocommand simply prints the text in the quotes to the terminal. We're using it here to "prompt" the user so they know what to do.read num1
Thereadcommand waits for the user to type something and press Enter. It then stores whatever the user typed into a variable callednum1.if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
This is the heart of our script. It's a conditional test.if [ ... ]: This is the basic syntax for an "if" statement.$num1: The$symbol retrieves the value stored in thenum1variable.-gt: This is a comparison operator that means "greater than". (Note: We use-gtfor numbers, not>which is for file redirection).&&: This is a logical "AND". It means *both* conditions must be true.- In plain English, this line says: "If the value of num1 is greater than num2 AND the value of num1 is also greater than num3, then..."
max=$num1
If the "if" statement is true, the script runs this line. It creates a new variable calledmaxand assigns it the value ofnum1.elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
elifis short for "else if". If the first "if" statement was false, the script tries this one. In plain English: "Else if the value of num2 is greater than num1 AND num2 is also greater than num3, then..."max=$num2
If the "elif" statement is true, we assign the value ofnum2to themaxvariable.else
This is the "catch-all" block. If the first "if" statement was false *and* the "elif" statement was also false, the script will run this. By process of elimination, if neithernum1nornum2is the maximum,num3must be.max=$num3
Assigns the value ofnum3to themaxvariable.fi
This simply means "end if". It'sifspelled backward and it closes the entire conditional block.echo "The maximum... is: $max"
Finally, we print the result. The shell automatically replaces$num1,$num2,$num3, and$maxwith the values stored in those variables.
How to Run the Script
Ready to try it? Here's how to run it on any Linux or macOS machine.
Step 1: Create the file
Open your terminal and use a text editor like nano to create a new file:
nano find_max.sh
Step 2: Paste the code
Copy the entire shell script from the first section and paste it into the nano editor.
Step 3: Save and Exit
In nano, press Ctrl+O, then Enter to save. Press Ctrl+X to exit.
Step 4: Make the script executable
By default, new files don't have permission to run as a program. We need to add that permission using the chmod command:
chmod +x find_max.sh
Step 5: Run it!
Now you can run your script by typing:
./find_max.sh
Example Output
When you run the script, you should see something like this:
$ ./find_max.sh
Enter the first number:
42
Enter the second number:
18
Enter the third number:
77
Calculating...
The maximum of the three numbers (42, 18, 77) is: 77
Run it again to test another case:
$ ./find_max.sh
Enter the first number:
101
Enter the second number:
50
Enter the third number:
99
Calculating...
The maximum of the three numbers (101, 50, 99) is: 101
Challenge Yourself!
And there you have it! You've successfully written a shell script to get user input, use conditional logic, and find a maximum value.
Want to take it a step further? Try modifying this script to find the minimum number instead. (Hint: the operator for "less than" is -lt).
Happy scripting!

0 Comments