0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Generate System Report with CPU, Memory, Disk Info

Use a Bash script with commands like 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

InputRun script on a Linux server
OutputSystem Report Linux hostname 5.15.0-60-generic #66-Ubuntu SMP ... 10:22:33 up 5 days, 3:14, 2 users, load average: 0.00, 0.01, 0.05 total used free shared buff/cache available Mem: 7.7G 1.2G 5.5G 80M 1.0G 6.1G Swap: 2.0G 0B 2.0G Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 15G 33G 31% / tmpfs 3.9G 0 3.9G 0% /dev/shm
InputRun script on minimal system
OutputSystem Report Linux minimal 4.19.0-17-amd64 #1 SMP Debian ... 11:00:00 up 1:00, 1 user, load average: 0.05, 0.10, 0.15 total used free shared buff/cache available Mem: 1.0G 200M 700M 10M 100M 800M Swap: 512M 0B 512M Filesystem Size Used Avail Use% Mounted on /dev/sdb1 20G 5G 14G 27% /
InputRun script on system with no swap
OutputSystem Report Linux noswap 5.10.0-8-amd64 #1 SMP Ubuntu ... 12:30:00 up 10 days, 3 users, load average: 0.20, 0.15, 0.10 total used free shared buff/cache available Mem: 16G 4G 10G 50M 2G 12G Swap: 0B 0B 0B Filesystem Size Used Avail Use% Mounted on /dev/nvme0n1p1 100G 40G 55G 43% /
🧠

How to Think About It

To create a system report, gather key system information like kernel details, uptime, memory usage, and disk space. Use simple Bash commands that show these details and print them in a readable format. Combine these commands in a script that runs them one after another.
📐

Algorithm

1
Print a header for the system report.
2
Get and print system information using <code>uname -a</code>.
3
Get and print system uptime using <code>uptime</code>.
4
Get and print memory usage using <code>free -h</code>.
5
Get and print disk usage using <code>df -h</code>.
💻

Code

bash
#!/bin/bash

echo "System Report"
uname -a
uptime
free -h
df -h
Output
System Report Linux hostname 5.15.0-60-generic #66-Ubuntu SMP Fri May 5 12:00:00 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux 10:22:33 up 5 days, 3:14, 2 users, load average: 0.00, 0.01, 0.05 total used free shared buff/cache available Mem: 7.7G 1.2G 5.5G 80M 1.0G 6.1G Swap: 2.0G 0B 2.0G Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 15G 33G 31% / tmpfs 3.9G 0 3.9G 0% /dev/shm
🔍

Dry Run

Let's trace the script running on a system with 8GB RAM and 100GB disk.

1

Print header

Output: System Report

2

Run uname -a

Output: Linux myhost 5.15.0-60-generic #66-Ubuntu SMP x86_64 GNU/Linux

3

Run uptime

Output: 11:00:00 up 2 days, 4:00, 1 user, load average: 0.05, 0.10, 0.15

StepCommandOutput Sample
1echo "System Report"System Report
2uname -aLinux myhost 5.15.0-60-generic ...
3uptime11: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

Use lshw for detailed hardware info
bash
#!/bin/bash
 echo "Detailed Hardware Report"
 lshw -short
Provides detailed hardware info but requires root and is slower.
Use top command snapshot
bash
#!/bin/bash
 echo "System Snapshot"
 top -b -n1 | head -20
Shows current processes and resource usage but less summary style.
Use vmstat for system stats
bash
#!/bin/bash
 echo "VMStat Report"
 vmstat 1 5
Gives performance stats over time but less user-friendly summary.

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.

ApproachTimeSpaceBest For
Basic commands (uname, uptime, free, df)O(1)O(1)Quick summary report
lshw detailed hardwareSlowerO(1)Detailed hardware info
top snapshotO(1)O(1)Current process and resource snapshot
vmstat statsO(1)O(1)Performance stats over time
💡
Make your script executable with chmod +x script.sh and run it with ./script.sh.
⚠️
Forgetting to add #!/bin/bash at the top can cause the script to fail or run with the wrong shell.