Skip to content

Latest commit

 

History

History
376 lines (291 loc) · 8.87 KB

File metadata and controls

376 lines (291 loc) · 8.87 KB

🎛️ System Dashboard (dashboard.sh)

A Bash command-line dashboard that provides an interactive menu-driven interface to access weather information, system information, and network information scripts.

This project demonstrates shell scripting fundamentals including loops, case statements, input validation, function creation, and creating a cohesive user interface that integrates multiple scripts.


📌 Project Description

dashboard.sh is the main entry point for a system dashboard that:

  • Displays an interactive menu with multiple options
  • Validates user input and handles errors gracefully
  • Executes different scripts based on user selection
  • Provides a seamless user experience with menu navigation
  • Uses reusable functions to keep code DRY (Don't Repeat Yourself)

The dashboard integrates three separate scripts:

  1. Weather Check (scripts/weather.sh) - Fetches weather information for a city
  2. System Info (scripts/sysinfo.sh) - Displays system information
  3. Network Info (scripts/network_info.sh) - Shows network information

🛠️ Requirements

Ensure the following tools are installed:

  • Bash
  • curl – for weather API requests (used by scripts/weather.sh)
  • jq – for JSON parsing (used by scripts/weather.sh)

The following scripts must be in the scripts/ directory:

  • scripts/weather.sh
  • scripts/sysinfo.sh
  • scripts/network_info.sh

🚀 How to Run the Script

1️⃣ Navigate to the project directory

cd shell-script-dashboard

2️⃣ Give execute permission to the scripts

chmod +x dashboard.sh
chmod +x scripts/*.sh

3️⃣ Run the script

./dashboard.sh

🧪 Example Output

Initial Menu Display

============================
    SYSTEM DASHBOARD
============================
    1. Check weather
    2. View System Info
    3. View Network Info
    4. Exit
============================

Enter your choice (1-4): 

Invalid Input Handling

Non-numeric input:

Enter your choice (1-4): l
Invalid choice! Please enter a number between 1 and 4.

Enter your choice (1-4): 

Out of range numeric input:

Enter your choice (1-4): 7
Invalid choice! Please enter a number between 1 and 4.

Enter your choice (1-4): 

After Completing an Action

Enter your choice (1-4): 1
Enter your city: Lagos
Getting Weather data for Lagos...

    ==== Weather Information for Lagos ====
        City: Lagos
        Temperature: 14°C
        Condition: Cloudy
        Humidity: 87%
        Wind Speed: 4km/h
    =======================================

Press enter to continue...

============================
    SYSTEM DASHBOARD
============================
    1. Check weather
    2. View System Info
    3. View Network Info
    4. Exit
============================

Enter your choice (1-4): 

Exiting the Dashboard

Enter your choice (1-4): 4
Goodbye!

🧠 Key Concepts Implemented

  • Infinite loops (while true) for continuous menu display
  • Case statements for menu option handling
  • Input validation with conditional checks and regex pattern matching
  • Numeric validation using regex to ensure only numbers are accepted
  • Function creation for reusable code (DRY principle)
  • Command execution of external scripts
  • User experience with "press enter to continue" prompts
  • Graceful error handling for invalid inputs (numeric and range validation)
  • Loop control with continue and exit

⚠️ Challenges Faced & How They Were Solved

1. Making the Terminal Prompt Loop Back Instead of Terminating

Problem: When users entered an invalid choice (less than 1 or greater than 4), the script would terminate instead of allowing them to try again.

Initial Code:

if [ $choice -lt 1 ] || [ $choice -gt 4 ]; then
    echo "You have reached the maximum number of queries ($max). Goodbye!"
    exit 0
fi

Solution: Wrapped the entire menu logic in a while true loop and used continue to loop back to the prompt:

while true; do
    read -p "Enter your choice (1-4): " choice
    
    if [ $choice -lt 1 ] || [ $choice -gt 4 ]; then
        echo "Invalid choice! Please enter a number between 1 and 4."
        echo ""
        continue
    fi
    # ... rest of the code
done

2. Stopping the Dashboard Menu from Repeating After First Iteration

Problem: The full dashboard menu was appearing on every iteration, making the output cluttered and repetitive.

Solution: Displayed the menu once at the start, then used a simple prompt for subsequent iterations:

# Show menu once at the start
echo "$DASHBOARD"

while true; do
    read -p "Enter your choice (1-4): " choice
    # ... rest of the code
done

3. Showing Dashboard Menu Again After Completing a Choice

Problem: After completing an action (like checking weather), the menu didn't reappear, leaving users unsure of what to do next.

Solution: Added code to display the dashboard menu after each script execution:

case $choice in
    1)
        ./scripts/weather.sh
        echo ""
        echo "$DASHBOARD"
        ;;
    # ... other cases
esac

4. Adding "Press Enter to Continue" Before Menu Display

Problem: The menu appeared immediately after script output, not giving users time to read the results.

Solution: Added a "Press enter to continue" prompt before showing the menu:

case $choice in
    1)
        ./scripts/weather.sh
        echo ""
        read -p "Press enter to continue..."
        echo ""
        echo "$DASHBOARD"
        ;;
    # ... other cases
esac

5. Making the Code DRY (Don't Repeat Yourself)

Problem: The same code pattern (empty line, "press enter" prompt, empty line, menu display) was repeated in three different case branches, violating the DRY principle.

Initial Code:

case $choice in
    1)
        ./scripts/weather.sh
        echo ""
        read -p "Press enter to continue..."
        echo ""
        echo "$DASHBOARD"
        ;;
    2)
        ./scripts/sysinfo.sh
        echo ""
        read -p "Press enter to continue..."
        echo ""
        echo "$DASHBOARD"
        ;;
    3)
        ./scripts/network_info.sh
        echo ""
        read -p "Press enter to continue..."
        echo ""
        echo "$DASHBOARD"
        ;;
esac

Solution: Created a reusable function to eliminate code duplication:

# Function to show "press enter" prompt and display dashboard menu
show_menu_again() {
    echo ""
    read -p "Press enter to continue..."
    echo ""
    echo "$DASHBOARD"
}

# Then use it in the case statement
case $choice in
    1)
        ./scripts/weather.sh
        show_menu_again
        ;;
    2)
        ./scripts/sysinfo.sh
        show_menu_again
        ;;
    3)
        ./scripts/network_info.sh
        show_menu_again
        ;;
esac

Benefits:

  • Code is more maintainable (change behavior in one place)
  • Cleaner and easier to read
  • Follows best practices for code organization

6. Validating Numeric Input

Problem: When users entered non-numeric values (like letters or special characters), the script would throw an error because it tried to compare strings with numeric operators (-lt, -gt).

Error Example:

Enter your choice (1-4): l
./dashboard.sh: line 25: [: l: integer expression expected
./dashboard.sh: line 25: [: l: integer expression expected

Initial Code:

if [ $choice -lt 1 ] || [ $choice -gt 4 ]; then
    echo "Invalid choice! Please enter a number between 1 and 4."
    echo ""
    continue
fi

Solution: Added numeric validation using regex pattern matching before the range check:

# Check if input is numeric
if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
    echo "Invalid choice! Please enter a number between 1 and 4."
    echo ""
    continue
fi

# Check if number is in valid range
if [ $choice -lt 1 ] || [ $choice -gt 4 ]; then
    echo "Invalid choice! Please enter a number between 1 and 4."
    echo ""
    continue
fi

Explanation:

  • [[ "$choice" =~ ^[0-9]+$ ]] uses regex to check if the input contains only digits
  • ^[0-9]+$ means: start of string, one or more digits, end of string
  • The ! negates the condition, so it triggers if the input is NOT numeric
  • This validation happens before the numeric comparison, preventing the error

Benefits:

  • Prevents script errors from non-numeric input
  • Provides clear error messages to users
  • Handles edge cases gracefully (letters, special characters, empty input)

📈 What This Project Demonstrates

  • Creating interactive CLI applications
  • Menu-driven interface design
  • Input validation and error handling
  • Function creation and code reusability
  • Integrating multiple scripts into a cohesive application
  • User experience considerations in terminal applications
  • Loop control and program flow management