Shell Script to Display Multiplication Table of Given Number - Easy Code Example
Ever needed to display a multiplication table for a specific number? Instead of manually calculating it, you can create a shell script to do the heavy lifting. This not only saves time but also ensures accuracy. Let's explore how to create a shell script for this task.
Before we dive into the script, let's ensure you're familiar with basic shell scripting concepts. If not, don't worry, we'll keep it beginner-friendly.
Understanding Shell Scripting
Shell scripting is a way to automate tasks on Unix-based systems (like Linux or macOS). It's a powerful tool for developers and system administrators alike. The shell script we'll create will be written in Bash, the default shell on most Linux systems.
Bash scripts are plain text files that contain a sequence of commands. These commands are executed in the order they appear in the file. To create a script, you'll need a text editor like nano, vim, or emacs.
Bash Script Basics
Here are some basic Bash commands you'll need to understand:
- echo: Displays a line of text
- read: Reads a line from the user
- for: Executes a block of code for each item in a list
- while: Executes a block of code while a condition is true
Running a Bash Script
To run a Bash script, you need to make it executable. You can do this with the chmod command. For example, to make a file named script.sh executable, you would run:
chmod +x script.sh
Then, you can run the script with ./, like so: ./script.sh
Creating the Multiplication Table Script
Now that we've covered the basics, let's create our multiplication table script. We'll use a loop to iterate through the numbers and display the results.
Here's a simple script that does this:
#!/bin/bash
# Prompt the user for a number
read -p "Enter a number: " num
# Check if the input is a number
re='^[0-9]+$'
if ! [[ $num =~ $re ]] ; then
echo "Error: Not a number" >&2; exit 1
fi
# Print the multiplication table
echo "Multiplication table for $num:"
for i in {1..10}
do
echo "$num * $i = $((num*i))"
done
Explanation of the Script
The script starts by prompting the user to enter a number. It then checks if the input is a valid number. If it's not, the script exits with an error message.
The script then prints a header for the multiplication table. It uses a for loop to iterate through the numbers 1 to 10. For each number, it calculates the product with the user's input and displays the result.
Running the Script
To run the script, save it to a file (e.g., multiply.sh), make it executable with chmod +x multiply.sh, and then run it with ./multiply.sh. When prompted, enter a number, and the script will display the multiplication table for that number.
And there you have it! You've just created a shell script to display a multiplication table. This script is a great example of how shell scripting can automate simple tasks, saving you time and effort.
Now that you've mastered this, why not explore more complex scripts? The possibilities are endless. Happy scripting!