write a shell script to display multiplication table of given number

Write a Shell Script to Display Multiplication Table of Given Number – Easy Guide

By John Miller
AD

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.

AD
Table of 23 - Multiplication Tables
Table of 23 - Multiplication Tables
a table that has numbers on it and the words'multiplication tables '
a table that has numbers on it and the words'multiplication tables '
Multiplication Tables (2, 3, 4) worksheet
Multiplication Tables (2, 3, 4) worksheet

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:

Tablas de multiplicar
Tablas de multiplicar
Multiplication Tables - 31 to 40
Multiplication Tables - 31 to 40
2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
```bash #!/bin/bash ```

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:

AD
```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.

Multiplication Tables and Charts
Multiplication Tables and Charts
Multiplication Chart 1 to 15 table for Kids [Free Printable ]
Multiplication Chart 1 to 15 table for Kids [Free Printable ]
a poster with numbers on it that says,'multiplications'in black and white
a poster with numbers on it that says,'multiplications'in black and white
Multiplication Strategies
Multiplication Strategies
two times tables with numbers and the same number
two times tables with numbers and the same number
a table with numbers and times on it
a table with numbers and times on it

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:

AD
```bash if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi ```

Next, 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.

18 Printable Multiplication Charts (Free PDF Download)
18 Printable Multiplication Charts (Free PDF Download)
14K views · 752 reactions | Multiplication table 1 and 2 chart | fun with maths #multiple #tables #maths #shorts #shortreels #viralreelsfb #familyday | Anas Creativity | Facebook
14K views · 752 reactions | Multiplication table 1 and 2 chart | fun with maths #multiple #tables #maths #shorts #shortreels #viralreelsfb #familyday | Anas Creativity | Facebook
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
Tables 1 to 12 – Multiplication Tables 1 to 12 » Onlymyenglish.com
Tables 1 to 12 – Multiplication Tables 1 to 12 » Onlymyenglish.com
a white board with numbers and times on it next to a sign that says 3 digit by 2 digit
a white board with numbers and times on it next to a sign that says 3 digit by 2 digit
Free Printable Multiplication Table 1-100 Chart
Free Printable Multiplication Table 1-100 Chart
Division Rules Anchor Chart, Multiplication And Division Anchor Chart 5th Grade, Multiplication And Division Anchor Chart 4th Grade, Multi Digit Division Anchor Chart, Multiples Anchor Chart 4th Grade, 4th Grade Division Anchor Chart, Dividing Multi Digit Numbers Anchor Chart, 5th Grade Math T-chart, Daily 5 Anchor Charts Pdf
Division Rules Anchor Chart, Multiplication And Division Anchor Chart 5th Grade, Multiplication And Division Anchor Chart 4th Grade, Multi Digit Division Anchor Chart, Multiples Anchor Chart 4th Grade, 4th Grade Division Anchor Chart, Dividing Multi Digit Numbers Anchor Chart, 5th Grade Math T-chart, Daily 5 Anchor Charts Pdf
dandelions and dragonflies: Topic Charts Multiplication Properties Anchor Chart, Multiplication Properties Educational Poster, Partial Products Multiplication Anchor Chart 4th Grade, Multiplication Properties Chart, Commutative Property Of Multiplication Anchor Chart, Associative Property Of Multiplication Anchor Chart 3rd Grade, Properties Of Multiplication Poster, Anchor Chart For Distributive Property Of Multiplication, Prime Factorization Anchor Chart 6th Grade
dandelions and dragonflies: Topic Charts Multiplication Properties Anchor Chart, Multiplication Properties Educational Poster, Partial Products Multiplication Anchor Chart 4th Grade, Multiplication Properties Chart, Commutative Property Of Multiplication Anchor Chart, Associative Property Of Multiplication Anchor Chart 3rd Grade, Properties Of Multiplication Poster, Anchor Chart For Distributive Property Of Multiplication, Prime Factorization Anchor Chart 6th Grade
Engaging Box Method Multiplication Anchor Chart for 4th Grade Math
Engaging Box Method Multiplication Anchor Chart for 4th Grade Math
multiplication tables Archives - Study Hut Tutoring
multiplication tables Archives - Study Hut Tutoring
Multiplication Tables (5, 6, 7) worksheet
Multiplication Tables (5, 6, 7) worksheet
a piece of paper that has some writing on it with numbers and times in them
a piece of paper that has some writing on it with numbers and times in them
a piece of paper with numbers and times on it that says ways to show multiplication
a piece of paper with numbers and times on it that says ways to show multiplication
Engaging Hands-On Multiplication Activities for Class 1: Mastering By 2s and 4s
Engaging Hands-On Multiplication Activities for Class 1: Mastering By 2s and 4s
AD