Skip to content

ifeDevops-qa/linux_shell_Scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

LINUX SHELL SCRIPTING

Table of Contents

  1. Project Overview
  2. Project Steps and Commands
  3. Script Implementation Steps
  4. Troubleshooting (Common Issues and Solutions)

1. Project Overview

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 seq command.
  • 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.


2. Project Steps and Commands

2.1. Create Project Directory

Create a directory to store your project files.

mkdir linux-shell-scripting
cd linux-shell-scripting

Project Directory

2.2. Initialize Git Repository

Initialize a Git repository in the project directory.

git init

Git Init

2.3. Create the Bash Script File (.sh)

Create a shell script file named multiplication_table.sh.

touch multiplication_table.sh

Create file

2.4. Make the Script Executable

Grant execution permissions to the script.

chmod +x multiplication_table.sh

File executable

2.5. Set Up GitHub Repository and Link Remote

Create a new repository on GitHub and link your local repository to it.

git remote add origin <repository_url>

Create GitHub repo


3. Script Implementation Steps

3.1. Prompt for User Input

Prompt the user to enter the number for which the multiplication table will be generated.

echo "Enter a number:"
read number

3.2. Ask for Table Type (Full or Partial)

Ask 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_type

3.3. Full Multiplication Table Using List-Style Loop

If 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"
    done

3.4. Partial Multiplication Table Using List-Style Loop

For 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"
        done

3.5. Full Multiplication Table Using C-Style Loop

Alternatively, 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"
    done

3.6. Partial Multiplication Table Using C-Style Loop

Similarly, 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"
    done

Note: Ensure that the range validation (start ≤ end) is performed before using the loops.

3.7. Input Validation and Handling Invalid Options

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

Implementation Overview


4. Troubleshooting (Common Issues and Solutions)

4.1. Script Permission Denied

  • Issue: Running the script may return a “Permission Denied” error.
  • Solution: Ensure the script is executable:
    chmod +x multiplication_table.sh

4.2. Invalid Input for Number

  • 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

4.3. Invalid Range in Partial Table

  • 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

4.4. Git Remote Connection Issues

  • Issue: Problems pushing code to GitHub.
  • Solution: Check your remote URL:
    git remote -v
    If necessary, re-add the remote:
    git remote add origin <repository_url>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages