90 Snippets: Still writing in shell ๐Ÿง™โ€โ™‚๏ธ

90 Snippets: Still writing in shell ๐Ÿง™โ€โ™‚๏ธ

90 Projects towards mastery - Day 05 ๐Ÿ’ฅ


3 min read

Greetings, Hashnode community,

This is Astro checking in again. Today is the end of the sixth day of the 90 Snippets challenge.

You may have seen the subtitle saying "Day 05." Let me explain this interesting point. I've decided to start counting days from zero, so the first day is "Day 00."

With each passing day, I find myself growing more confident in my coding prowess. The Linux command line, once an enigma, now feels like home. Today's mission? Crafting a system status report script that effortlessly gathers vital information about the host system.

The Day's Goal

Today, our goal is to build a Bash script that extracts system information, CPU details, memory usage, disk statistics, and network configuration. The script should be a versatile companion, able to display the system report on the terminal or save it to a file as needed.

The full shell script is right here

Added Value: Code Breakdown

Let's dive into the script's magical incantations, one code snippet at a time:

  1. Handling Help Option:
if [ $1 == "-h" -o $1 == "--help" -o $1 == "help" ]; then
    # Display usage information
    exit 0

In the quest for user-friendly scripts, I've added a help option. Run the script with -h, --help, or simply help, and you'll be granted the wisdom of usage instructions.

  1. Gathering System Information:
kernel_version=$(uname -r)
os_version=$(sw_vers -productVersion)

These commands are like magical spells. They extract the kernel version, OS version, hostname, and uptime, granting us a window into the system's heart.

  1. CPU Enchantment:
cpu_info=$(sysctl -n machdep.cpu.brand_string)
cpu_cores=$(sysctl -n machdep.cpu.core_count)
cpu_threads=$(sysctl -n machdep.cpu.thread_count)

With these incantations, we unveil the CPU's true identity โ€“ its brand, core count, and thread count.

  1. Memory Magic:
memory_info=$(sysctl -n hw.memsize)
memory_info=$((memory_info / 1024 / 1024))

Transforming the arcane memory size into human-readable MB units, this code lets us grasp the system's memory at a glance.

  1. Disk Divination:
disk_usage=$(df -h /)

A spell that reveals the disk's usage, showing available, used, and total space.

  1. Network Revelation:
ip_address=$(ipconfig getifaddr en0)
gateway=$(netstat -nr | grep default | awk '{print $2}')
dns_servers=$(scutil --dns | grep "nameserver\[[0-9]\]" | awk '{print $3}')

These commands unveil the network's mysteries, revealing IP addresses, gateways, and DNS servers.

The Grand Finale

The script elegantly orchestrates all this data into a magnificent system status report. Whether you choose to display it on the terminal or save it to a file, your quest for knowledge is fulfilled.

By Thor, By Odin, Let's Conquer

With the power of shell scripting coursing through our veins, we've conquered yet another challenge. Day 05's journey was not just about data extraction; it was about embracing newfound confidence in the world of code.

Stay tuned for more chapters in this epic tale of self-improvement through shell scripting. Until then, remember: By Thor, By Odin, let's hack this world into oblivion!

Keep scripting, and may your terminal always respond in your favor.