In this project, you will create a Bash script that gathers and displays various system information. This project will help you consolidate your knowledge of Bash scripting and apply it to a practical task.
Objectives
- Gather system information using Bash commands.
- Format and display the information in a readable manner.
- Practice using variables, conditional statements, and loops.
Key Concepts
Before we start, let's review some key concepts that will be useful for this project:
- Variables: Store data that can be used and manipulated within the script.
- Commands: Use built-in Bash commands to gather system information.
- Conditional Statements: Make decisions based on certain conditions.
- Loops: Repeat a set of commands multiple times.
Step-by-Step Guide
Step 1: Create the Script File
First, create a new Bash script file. You can name it system_info.sh
.
Step 2: Add the Shebang
Add the shebang at the top of the script to specify the interpreter.
Step 3: Gather System Information
Use various Bash commands to gather system information. Store the results in variables.
#!/bin/bash # Gather system information HOSTNAME=$(hostname) UPTIME=$(uptime -p) KERNEL=$(uname -r) CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') DISK=$(df -h / | grep '/' | awk '{print $4}') IP=$(hostname -I | awk '{print $1}')
Step 4: Display the Information
Format and display the gathered information in a readable manner.
#!/bin/bash # Gather system information HOSTNAME=$(hostname) UPTIME=$(uptime -p) KERNEL=$(uname -r) CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') DISK=$(df -h / | grep '/' | awk '{print $4}') IP=$(hostname -I | awk '{print $1}') # Display the information echo "System Information" echo "==================" echo "Hostname: $HOSTNAME" echo "Uptime: $UPTIME" echo "Kernel Version: $KERNEL" echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Available Disk Space: $DISK" echo "IP Address: $IP"
Step 5: Add Comments
Add comments to your script to explain each section.
#!/bin/bash # Gather system information HOSTNAME=$(hostname) # Get the hostname UPTIME=$(uptime -p) # Get the system uptime KERNEL=$(uname -r) # Get the kernel version CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) # Get the CPU model MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') # Get the total memory DISK=$(df -h / | grep '/' | awk '{print $4}') # Get the available disk space IP=$(hostname -I | awk '{print $1}') # Get the IP address # Display the information echo "System Information" echo "==================" echo "Hostname: $HOSTNAME" echo "Uptime: $UPTIME" echo "Kernel Version: $KERNEL" echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Available Disk Space: $DISK" echo "IP Address: $IP"
Step 6: Test the Script
Run the script to ensure it works correctly.
Step 7: Handle Errors (Optional)
Add error handling to manage potential issues, such as missing commands or permissions.
#!/bin/bash # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Check for required commands REQUIRED_COMMANDS=("hostname" "uptime" "uname" "lscpu" "free" "df" "awk" "grep" "xargs") for cmd in "${REQUIRED_COMMANDS[@]}"; do if ! command_exists "$cmd"; then echo "Error: $cmd is not installed." >&2 exit 1 fi done # Gather system information HOSTNAME=$(hostname) UPTIME=$(uptime -p) KERNEL=$(uname -r) CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') DISK=$(df -h / | grep '/' | awk '{print $4}') IP=$(hostname -I | awk '{print $1}') # Display the information echo "System Information" echo "==================" echo "Hostname: $HOSTNAME" echo "Uptime: $UPTIME" echo "Kernel Version: $KERNEL" echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Available Disk Space: $DISK" echo "IP Address: $IP"
Practical Exercises
Exercise 1: Add More Information
Expand the script to include additional system information, such as:
- Number of running processes
- Logged-in users
- System load average
Solution:
#!/bin/bash # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Check for required commands REQUIRED_COMMANDS=("hostname" "uptime" "uname" "lscpu" "free" "df" "awk" "grep" "xargs" "ps" "who" "uptime") for cmd in "${REQUIRED_COMMANDS[@]}"; do if ! command_exists "$cmd"; then echo "Error: $cmd is not installed." >&2 exit 1 fi done # Gather system information HOSTNAME=$(hostname) UPTIME=$(uptime -p) KERNEL=$(uname -r) CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') DISK=$(df -h / | grep '/' | awk '{print $4}') IP=$(hostname -I | awk '{print $1}') PROCESSES=$(ps -e | wc -l) USERS=$(who | wc -l) LOAD_AVERAGE=$(uptime | awk -F'load average:' '{ print $2 }' | xargs) # Display the information echo "System Information" echo "==================" echo "Hostname: $HOSTNAME" echo "Uptime: $UPTIME" echo "Kernel Version: $KERNEL" echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Available Disk Space: $DISK" echo "IP Address: $IP" echo "Running Processes: $PROCESSES" echo "Logged-in Users: $USERS" echo "System Load Average: $LOAD_AVERAGE"
Exercise 2: Save Output to a File
Modify the script to save the output to a file named system_info.txt
.
Solution:
#!/bin/bash # Function to check if a command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Check for required commands REQUIRED_COMMANDS=("hostname" "uptime" "uname" "lscpu" "free" "df" "awk" "grep" "xargs" "ps" "who" "uptime") for cmd in "${REQUIRED_COMMANDS[@]}"; do if ! command_exists "$cmd"; then echo "Error: $cmd is not installed." >&2 exit 1 fi done # Gather system information HOSTNAME=$(hostname) UPTIME=$(uptime -p) KERNEL=$(uname -r) CPU=$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs) MEMORY=$(free -h | grep 'Mem:' | awk '{print $2}') DISK=$(df -h / | grep '/' | awk '{print $4}') IP=$(hostname -I | awk '{print $1}') PROCESSES=$(ps -e | wc -l) USERS=$(who | wc -l) LOAD_AVERAGE=$(uptime | awk -F'load average:' '{ print $2 }' | xargs) # Display the information { echo "System Information" echo "==================" echo "Hostname: $HOSTNAME" echo "Uptime: $UPTIME" echo "Kernel Version: $KERNEL" echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Available Disk Space: $DISK" echo "IP Address: $IP" echo "Running Processes: $PROCESSES" echo "Logged-in Users: $USERS" echo "System Load Average: $LOAD_AVERAGE" } > system_info.txt
Conclusion
In this project, you created a Bash script to gather and display system information. You practiced using variables, commands, conditional statements, and loops. You also learned how to handle errors and save output to a file. This project serves as a foundation for more complex Bash scripting tasks.
Bash Programming Course
Module 1: Introduction to Bash
Module 2: Basic Bash Commands
- File and Directory Operations
- Text Processing Commands
- File Permissions and Ownership
- Redirection and Piping