Corning Community College
CSCS1730 UNIX/Linux Fundamentals
To utilize bash scripting in order to achieve a desired ends: the generation of a multiplication table for use in a specified number base.
Everyone is familiar with the venerable multiplication table, that quintessential grade school aid that enabled us to more easily memorize our “multiplication facts”, supercharging our basic math abilities and understanding of numbers without the constant need of a calculator.
The standard table (for base 10) is as follows:
1 2 3 4 5 6 7 8 9 x--------------------------- 1 | 1 2 3 4 5 6 7 8 9 2 | 2 4 6 8 10 12 14 16 18 3 | 3 6 9 12 15 18 21 24 27 4 | 4 8 12 16 20 24 28 32 36 5 | 5 10 15 20 25 30 35 40 45 6 | 6 12 18 24 30 36 42 48 54 7 | 7 14 21 28 35 42 49 56 63 8 | 8 16 24 32 40 48 56 64 72 9 | 9 18 27 36 45 54 63 72 81
Where we line up on the heading row/column the two numbers we wish to multiply, and its product at the intersection of those two within the body of the table.
For example, 6 x 7 is 42, as you should be able to find (twice: 6×7 and 7×6).
While you've likely had little need, due to your solid memorization and recall, for years of the base 10 multiplication table, we're going to bring it back to the forefront, like many good mental aids, in that they make for excellent exercises to have the computer generate.
Your task for this project is to write a bash script to generate a multiplication table.
If run without any arguments, the default action is to display a base 10 table from 1 to 9.
The table itself:
Additionally, with the specification of command-line arguments, we can change:
NOTE that the ending value comes before the starting value.
Some commands you will want to explore and reference the manual pages of:
We have been using the bash shell on lab46 and our pi systems this semester, so in many ways, the various “steps” files you've created have largely been bash scripts. There are a few formalities to really ensure that is the case.
One thing that makes a bash script an actual bash script is the shabang, a mechanism for ensuring our script is run using a particular shell.
The VERY FIRST LINE of our script, to set this up, will therefore be as follows:
#!/usr/bin/env bash
Commonly considered good practice, our script will end with a call to the shell's exit command, sending back a status to the operating system (in our case, a 0 indicating successful operation).
exit 0
Your script will then occur between the shabang and this final exit command.
Bash provides us a number of looping facilities, and for this project we will be making use specifically of the for loop.
We have the typical numeric for loop (conforming largely to as you'd see it in various programming languages):
for ((index=0; index < 9; index++)); do echo "index is ${index}" done
And also a list-based for loop:
LIST="1 2 4 8 16 32 64 128 256" for item in ${LIST}; do echo "item is ${item}" done
bc is a powerful text-based calculator, which will provide the core of our mathematical and number representation functionality for this project.
lab46:~/src/unix/mtf0$ bc -q 4*5 20
Because it takes input from STDIN, and sends output to STDOUT, it can be especially useful in a command pipeline.
By default, it expects input values to be in base 10, and outputs the result in base 10.
The printf tool is an especially powerful output formatter.