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.
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:
- Weather Check (
scripts/weather.sh) - Fetches weather information for a city - System Info (
scripts/sysinfo.sh) - Displays system information - Network Info (
scripts/network_info.sh) - Shows network information
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.shscripts/sysinfo.shscripts/network_info.sh
cd shell-script-dashboardchmod +x dashboard.sh
chmod +x scripts/*.sh./dashboard.sh============================
SYSTEM DASHBOARD
============================
1. Check weather
2. View System Info
3. View Network Info
4. Exit
============================
Enter your choice (1-4):
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):
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):
Enter your choice (1-4): 4
Goodbye!
- 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
continueandexit
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
fiSolution:
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
doneProblem: 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
doneProblem: 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
esacProblem: 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
esacProblem: 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"
;;
esacSolution: 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
;;
esacBenefits:
- Code is more maintainable (change behavior in one place)
- Cleaner and easier to read
- Follows best practices for code organization
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
fiSolution: 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
fiExplanation:
[[ "$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)
- 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