Write a Shell Script to Display Multiplication Table of Given Number – Easy Guide
Ever found yourself needing to quickly generate a multiplication table for a given number? While there are numerous online tools and calculators available, sometimes you might want to create a simple script to accomplish this task, especially if you're working in a Linux environment. In this guide, we'll walk you through the process of writing a shell script to display a multiplication table of a given number.
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 a powerful tool for automating repetitive tasks and streamlining workflows. Now, let's get started with creating our multiplication table script.
Setting Up the Shell Script
To create a shell script, you'll need a text editor. For this guide, we'll use nano, a simple and user-friendly text editor that comes pre-installed on most Linux distributions.
First, open your terminal and create a new file named `multiplication.sh` using the following command:
```bash nano multiplication.sh ```Defining the Script Header
At the top of the file, add the following line to ensure your script is executable and uses the bash shell:
This line tells the system to interpret the script using the bash shell. Now, let's move on to creating the multiplication table functionality.
Creating the Multiplication Function
Below the header, define a function named `multiply` that takes two arguments: the number for which you want to generate the multiplication table and the maximum multiple. Here's the function definition:
```bash multiply() { num=$1 max=$2 echo "Multiplication table for $num:" for i in $(seq 1 $max); do echo "$num * $i = $((num*i))" done } ```This function uses a `for` loop to iterate from 1 to the specified maximum multiple. For each iteration, it calculates the product of the given number and the current multiple, then prints the result in a formatted string.
Adding Input Validation and User Interaction
Before we call our `multiply` function, let's add some input validation and user interaction to make our script more robust and user-friendly.
First, check if the user has provided two arguments. If not, display a usage message and exit the script:
```bash if [ "$#" -ne 2 ]; then echo "Usage: $0Next, ensure that both arguments are positive integers. If either argument is not a positive integer, display an error message and exit the script:
```bash if ! [[ "$1" =~ ^[0-9]+$ ]] || ! [[ "$2" =~ ^[0-9]+$ ]]; then echo "Error: Both arguments must be positive integers." exit 1 fi ```Calling the Multiplication Function
Now that we've validated the input, we can call our `multiply` function with the user-provided arguments:
```bash multiply $1 $2 ```With this line, our script is complete. Save and close the file by pressing `Ctrl+X`, then `Y`, and finally `Enter`.
Making the Script Executable
To make the script executable, use the following command:
```bash chmod +x multiplication.sh ```Now you can run the script from the command line by providing the desired number and maximum multiple as arguments:
```bash ./multiplication.sh 5 10 ```This will display the multiplication table for the number 5 up to the 10th multiple.
In this guide, we've created a simple yet powerful shell script to generate multiplication tables. This script demonstrates the basics of shell scripting, including function definition, input validation, and user interaction. With this knowledge, you can expand upon this script or create new ones to automate other tasks in your Linux environment.