Below is an example of a Bash shell script that gathers various system hardware and software components from a Linux PC. This script uses standard commands such as uname, lscpu, free, lspci, lsusb, and others to display information about your PC. Note that some commands (like dmidecode) may require root privileges.
Save the following content into a file (for example, fetch_components.sh), then make it executable with chmod +x fetch_components.sh and run it.
#!/bin/bash
# Script: fetch_components.sh
# Description: Fetch and display various system components from a Linux PC.
echo "========================================="
echo " System Information"
echo "========================================="
echo "Hostname: $(hostname)"
echo "Kernel Info: $(uname -r)"
echo "Operating System: $(uname -o)"
echo
echo "========================================="
echo " CPU Information"
echo "========================================="
if command -v lscpu >/dev/null 2>&1; then
lscpu
else
echo "lscpu command not found."
fi
echo
echo "========================================="
echo " Memory Information"
echo "========================================="
if command -v free >/dev/null 2>&1; then
free -h
else
echo "free command not found."
fi
echo
echo "========================================="
echo " Block Devices"
echo "========================================="
if command -v lsblk >/dev/null 2>&1; then
lsblk
else
echo "lsblk command not found."
fi
echo
echo "========================================="
echo " PCI Devices"
echo "========================================="
if command -v lspci >/dev/null 2>&1; then
lspci
else
echo "lspci command not found."
fi
echo
echo "========================================="
echo " USB Devices"
echo "========================================="
if command -v lsusb >/dev/null 2>&1; then
lsusb
else
echo "lsusb command not found."
fi
echo
echo "========================================="
echo " Extended Hardware Info (using dmidecode)"
echo "========================================="
if command -v dmidecode >/dev/null 2>&1; then
# dmidecode typically requires root privileges.
if [ "$(id -u)" -ne 0 ]; then
echo "To display detailed hardware info, re-run as root or with sudo."
else
dmidecode
fi
else
echo "dmidecode not installed."
fi
echo
echo "========================================="
echo " End of System Components Report"
echo "========================================="
How the Script Works
-
System General Info:
It displays the hostname, kernel version, and operating system using commands like hostname and uname.
-
CPU and Memory:
It fetches CPU details with lscpu and memory usage with free -h.
-
Storage and Devices:
The script lists block devices using lsblk, PCI devices with lspci, and USB devices with lsusb.
-
Extended Hardware Details:
If available and executed as root, dmidecode provides detailed hardware information.
Usage Notes
-
Permissions:
Some commands (like dmidecode) might require root permissions. Run the script as sudo ./fetch_components.sh to view complete details.
-
Availability:
The script checks if each command exists; if not, it will notify you. You might need to install missing utilities using your distribution's package manager (for example, sudo apt-get install lscpu dmidecode on Debian-based systems).
This script provides a starting point to fetch various components from a PC. You can expand it further based on your specific requirements.