Bash Script to Generate System Report with CPU, Memory, Disk Info
uname -a, uptime, free -h, and df -h combined to generate a system report, for example: #!/bin/bash
echo "System Report"; uname -a; uptime; free -h; df -h.Examples
How to Think About It
Algorithm
Code
#!/bin/bash
echo "System Report"
uname -a
uptime
free -h
df -hDry Run
Let's trace the script running on a system with 8GB RAM and 100GB disk.
Print header
Output: System Report
Run uname -a
Output: Linux myhost 5.15.0-60-generic #66-Ubuntu SMP x86_64 GNU/Linux
Run uptime
Output: 11:00:00 up 2 days, 4:00, 1 user, load average: 0.05, 0.10, 0.15
| Step | Command | Output Sample |
|---|---|---|
| 1 | echo "System Report" | System Report |
| 2 | uname -a | Linux myhost 5.15.0-60-generic ... |
| 3 | uptime | 11:00:00 up 2 days, 4:00, 1 user, load average: 0.05, 0.10, 0.15 |
Why This Works
Step 1: Print header
The echo command prints a clear title so the report is easy to identify.
Step 2: Get system info
uname -a shows kernel and system details, giving a snapshot of the OS.
Step 3: Show uptime
uptime tells how long the system has been running and current load averages.
Step 4: Memory and disk usage
free -h and df -h show human-readable memory and disk usage for easy understanding.
Alternative Approaches
#!/bin/bash
echo "Detailed Hardware Report"
lshw -short#!/bin/bash echo "System Snapshot" top -b -n1 | head -20
#!/bin/bash echo "VMStat Report" vmstat 1 5
Complexity: O(1) time, O(1) space
Time Complexity
The script runs a fixed set of commands once, so time does not grow with input size.
Space Complexity
Uses minimal memory to store command outputs temporarily; no large data structures.
Which Approach is Fastest?
The basic commands used are fast and lightweight; alternatives like lshw are slower but more detailed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic commands (uname, uptime, free, df) | O(1) | O(1) | Quick summary report |
| lshw detailed hardware | Slower | O(1) | Detailed hardware info |
| top snapshot | O(1) | O(1) | Current process and resource snapshot |
| vmstat stats | O(1) | O(1) | Performance stats over time |
chmod +x script.sh and run it with ./script.sh.#!/bin/bash at the top can cause the script to fail or run with the wrong shell.