Write a Shell Script to Display Multiplication Table of Any Number Easily
Ever found yourself needing to quickly generate a multiplication table for a specific number? While there are numerous online tools and calculators available, sometimes you might want to create a simple, reusable script to accomplish this task. This is where shell scripting comes in handy. In this guide, we'll walk you through creating a shell script that displays the multiplication table of any number you input.
Before we dive into the script, let's ensure you have a basic understanding of shell scripting. Shell scripts are plain text files that contain a sequence of commands for a Unix-based operating system to execute. They are written in a scripting language built into the shell, which is a command-line interpreter or terminal. In this case, we'll be using Bash, a popular shell on Linux and macOS.
Setting Up the Shell Script
To create a shell script, you'll first need to open a text editor. For this guide, we'll use nano, a simple and widely available text editor. In your terminal, type:
This will open a new file named 'multiplication_table.sh' in the nano text editor.
Accepting User Input
Our script will start by accepting user input for the number whose multiplication table we want to generate. To do this, we'll use the `read` command in Bash. Add the following line to your script:
This line will display the prompt "Enter a number: " and store the user's input in the variable 'num'.
Validating User Input
Before generating the multiplication table, we should validate that the user has entered a positive integer. Add the following lines to your script:
```bash re='^[0-9]+$' if ! [[ $num =~ $re ]] ; then echo "Error: Not a number" >&2; exit 1 fi if [ $num -lt 1 ]; then echo "Error: Number must be positive" >&2; exit 1 fi ```These lines use regular expressions to check if the input is a number, and then ensure it's a positive integer. If the input is invalid, the script will display an error message and exit.
Generating the Multiplication Table
Now that we have a valid number, we can generate the multiplication table. We'll use a for loop to iterate through numbers 1 to 10 and calculate their product with the user's input. Add the following lines to your script:
```bash echo "Multiplication table for $num:" for i in {1..10} do echo "$num * $i = $((num*i))" done ```This will display the multiplication table for the input number, with each line showing the product of the number and the current iteration of the loop.
Making the Script Executable
Before you can run your script, you need to make it executable. In your terminal, type:
Now you can run your script with:
```bash ./multiplication_table.sh ```This will display the prompt "Enter a number: ", and after you input a number, it will generate and display the multiplication table for that number.
And there you have it! You've created a simple shell script that generates multiplication tables. This script is a great example of how shell scripting can automate repetitive tasks, making your work more efficient. Happy scripting!