Guide to a System Information Bash Script

Guide to a System Information Bash Script

Managing and monitoring your system is crucial for maintaining its health and performance. Whether you’re a system administrator or a regular user, knowing how to check your system’s resources can be a game-changer. The Bash script we’ll discuss here offers a user-friendly, menu-driven interface to display critical system information.

Overview of the Script

This Bash script provides a comprehensive menu to display various system details, such as CPU, memory, disk space, and network information. It allows users to interact with their system in an intuitive way, making it easier to gather important data without needing to remember specific commands.

Script Features

  1. User-Friendly Menu: The script displays a clear menu with options to choose from, ensuring ease of use.
  2. System Information: Quickly access information like hostname, uptime, kernel version, and OS type.
  3. Disk Space Usage: View the disk space utilization across mounted filesystems, excluding temporary filesystems and CD-ROM drives.
  4. Home Directory Space Utilization: Get detailed space usage of the home directory, either for the current user or all users if run as root.
  5. CPU Information: Retrieve detailed CPU information, including the model name, number of cores, and threads.
  6. Memory Information: Display memory usage statistics, including free memory and swap usage.
  7. Network Information: View network interface statuses and the default gateway.

How the Script Works

The script begins by defining a function called show_menu, which presents the user with a list of options to choose from. Each option corresponds to a specific system information task. When the user makes a selection, the script executes the relevant function.

Each function in the script is dedicated to gathering specific information:

  • System Information (system_info): Uses commands like uname, uptime, and /etc/os-release to display system details.
  • Disk Space (disk_space): Utilizes df -h to show disk usage while filtering out irrelevant filesystems.
  • Home Space Utilization (home_space): Runs du -sh to display disk usage for the home directories, varying by user privileges.
  • CPU Info (cpu_info): Employs lscpu to provide a summary of the CPU’s capabilities.
  • Memory Info (memory_info): Leverages free -h and swapon to report on RAM and swap usage.
  • Network Info (network_info): Uses ip commands to show network interfaces and routing details.

The script keeps running in a loop, allowing users to view different sets of information without restarting the script. It exits cleanly when the user chooses the option to quit.

Creating the Script

Creating this Bash script is straightforward. Follow these steps to create and use the script on your system:

  1. Open a Terminal: Start by opening a terminal window on your Linux system.
  2. Create a New Script File: Use a text editor like nano or vim to create a new Bash script file. For example:
nano sys-info-menu.sh

3. Copy the Script: Paste the following script into the file:

#!/usr/bin/env bash
# sys-info-menu: A menu driven system information program with enhanced functionality
# Author     : Fedya Serafiev

# Function to display the menu
show_menu() {
    clear
    echo "========================================"
    echo "            SYSTEM INFORMATION          "
    echo "========================================"
    echo "Please Select:"
    echo "    1. Display System Information"
    echo "    2. Display Disk Space"
    echo "    3. Display Home Space Utilization"
    echo "    4. Display CPU Info"
    echo "    5. Display Memory Info"
    echo "    6. Display Network Info"
    echo "    0. Quit"
    echo "========================================"
    echo -n "Enter selection [0-6]: "
}

# Function to display system information
system_info() {
    echo "Hostname: $HOSTNAME"
    echo "Uptime: $(uptime -p)"
    echo "Kernel Version: $(uname -r)"
    echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2)"
    echo "Architecture: $(uname -m)"
}

# Function to display disk space
disk_space() {
    df -h | grep -vE '^Filesystem|tmpfs|cdrom'
}

# Function to display home space utilization
home_space() {
    if [ "$UID" = 0 ]; then
        echo "Home Space Utilization (All Users):"
        du -sh /home/*
    else
        echo "Home Space Utilization ($USER):"
        du -sh "$HOME"
    fi
}

# Function to display CPU info
cpu_info() {
    echo "CPU Info:"
    lscpu | grep -E '^Model name|^CPU\(s\)|^Thread\(s\) per core|^Core\(s\) per socket|^Socket\(s\)'
}

# Function to display memory info
memory_info() {
    echo "Memory Info:"
    free -h
    echo "Swap Usage:"
    swapon --show=NAME,SIZE,USED,PRIO
}

# Function to display network info
network_info() {
    echo "Network Info:"
    ip -br a
    echo "Default Gateway:"
    ip r | grep default
}

# Main program loop
while true; do
    show_menu
    read -r sel
    echo
    case $sel in
        0) echo "Program terminated."; break;;
        1) system_info;;
        2) disk_space;;
        3) home_space;;
        4) cpu_info;;
        5) memory_info;;
        6) network_info;;
        *) echo "Invalid entry." >&2;;
    esac
    echo -n "Press any key to continue..."
    read -r -n 1
done

4. Save the File: Save and close the file.

5. Make the Script Executable: Change the file’s permissions to make it executable by running:

chmod +x sys-info-menu.sh

Using the Script

After creating the script, you can easily run it from your terminal. Here’s how to use it:

  1. Run the Script: Execute the script by typing:
./sys-info-menu.sh

  1. This command will display the menu interface, allowing you to choose from various system information options.
  2. Select an Option: Enter the number corresponding to the information you wish to view. For instance, pressing 1 will display general system information, while pressing 5 will show memory details.
  3. Exit the Script: To exit, simply select option 0 from the menu. The script will terminate and return you to the terminal prompt.

Benefits of Using This Script

  1. Simplicity: The menu-driven approach makes it accessible for users with varying levels of expertise.
  2. Efficiency: Instead of running multiple commands separately, users can quickly access all critical system information in one place.
  3. Customization: The script is easy to modify, allowing users to add more functions or refine existing ones.
  4. Improved System Monitoring: Regular use of the script can help in early detection of potential issues, such as low disk space or high memory usage.

Customizing the Script

Users can easily customize the script by adding new functions or modifying existing ones. For example, if you want to add an option to display the list of installed packages, you can create a new function that uses dpkg -l (for Debian-based systems) or rpm -qa (for Red Hat-based systems).

Conclusion

This Bash script is a powerful tool for anyone looking to monitor their system efficiently. Its menu-driven interface, combined with comprehensive system information, makes it an invaluable resource for both novice and experienced users. By regularly using this script, you can keep your system running smoothly and identify issues before they become critical.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: