- Project Overview
- Project Steps and Commands
- Script Implementation Steps
- 3.1 Prompt for User Input
- 3.2 Ask for Table Type (Full or Partial)
- 3.3 Full Multiplication Table Using List-Style Loop
- 3.4 Partial Multiplication Table Using List-Style Loop
- 3.5 Full Multiplication Table Using C-Style Loop
- 3.6 Partial Multiplication Table Using C-Style Loop
- 3.7 Input Validation and Handling Invalid Options
- Troubleshooting (Common Issues and Solutions)
This project demonstrates how to create a Bash script that generates a multiplication table for a user-specified number. The script showcases two loop techniques:
- List-Style For Loop: Uses sequence expansion or the
seqcommand. - C-Style For Loop: Uses the classic C-like loop syntax.
The user can choose between generating a full multiplication table (from 1 to 10) or a partial table over a custom range. The project is version-controlled with Git and pushed to GitHub.
Create a directory to store your project files.
mkdir linux-shell-scripting
cd linux-shell-scriptingInitialize a Git repository in the project directory.
git initCreate a shell script file named multiplication_table.sh.
touch multiplication_table.shGrant execution permissions to the script.
chmod +x multiplication_table.shCreate a new repository on GitHub and link your local repository to it.
git remote add origin <repository_url>Prompt the user to enter the number for which the multiplication table will be generated.
echo "Enter a number:"
read numberAsk whether the user wants a full table (1 to 10) or a partial table with a custom range.
echo "Do you want a full table or partial table? (Enter 'f' for full, 'p' for partial)"
read table_typeIf the user selects the full table option using list-style loops:
if [ "$table_type" == "f" ]; then
echo "Multiplication table for $number (List-style, Ascending):"
for i in {1..10}
do
result=$((number * i))
echo "$number x $i = $result"
doneFor a partial table, prompt the user for a range and then use a list-style loop:
elif [ "$table_type" == "p" ]; then
echo "Enter the start number of the range:"
read start
echo "Enter the end number of the range:"
read end
if [ "$start" -le "$end" ]; then
echo "Multiplication table for $number from $start to $end (List-style):"
for i in $(seq $start $end)
do
result=$((number * i))
echo "$number x $i = $result"
doneAlternatively, here’s how to implement the full multiplication table using a C-style for loop:
echo "Multiplication table for $number (C-style, Ascending):"
for (( i=1; i<=10; i++ ))
do
result=$((number * i))
echo "$number x $i = $result"
doneSimilarly, for a partial table with a custom range using a C-style loop:
echo "Multiplication table for $number from $start to $end (C-style):"
for (( i=start; i<=end; i++ ))
do
result=$((number * i))
echo "$number x $i = $result"
doneNote: Ensure that the range validation (start ≤ end) is performed before using the loops.
Add error handling for invalid inputs and ranges.
else
echo "Invalid range. The start number must be less than or equal to the end number."
fi
else
echo "Invalid option. Please enter 'f' for full table or 'p' for partial table."
fi- Issue: Running the script may return a “Permission Denied” error.
- Solution: Ensure the script is executable:
chmod +x multiplication_table.sh
- Issue: Non-numeric input can cause unexpected behavior.
- Solution: Add input validation:
if ! [[ "$number" =~ ^[0-9]+$ ]]; then echo "Invalid input. Please enter a valid number." exit 1 fi
- Issue: The start number is greater than the end number.
- Solution: Verify the range before generating the table:
if [ "$start" -gt "$end" ]; then echo "Invalid range. The start number must be less than or equal to the end number." exit 1 fi
- Issue: Problems pushing code to GitHub.
- Solution: Check your remote URL:
If necessary, re-add the remote:
git remote -v
git remote add origin <repository_url>




