Created
January 7, 2024 22:54
-
-
Save james-see/10b823e5edb940efefe17b33fdb1b1bb to your computer and use it in GitHub Desktop.
get system info for linux or mac os
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to check if a command exists | |
command_exists() { | |
type "$1" &> /dev/null | |
} | |
# Function to gather system information | |
function gather_system_info() { | |
info="System Information Report\n" | |
info+="Date: $(date)\n" | |
info+="------------------------------\n" | |
if command_exists hostname; then | |
info+="Hostname:\n$(hostname)\n" | |
info+="------------------------------\n" | |
fi | |
info+="System Overview:\n$(uname -a)\n" | |
info+="------------------------------\n" | |
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
# Linux specific commands | |
if command_exists lscpu; then | |
info+="CPU Information:\n$(lscpu)\n" | |
info+="------------------------------\n" | |
fi | |
if command_exists free; then | |
info+="Memory Information:\n$(free -h)\n" | |
info+="------------------------------\n" | |
fi | |
elif [[ "$OSTYPE" == "darwin"* ]]; then | |
# macOS specific commands | |
info+="CPU Information:\n$(sysctl -n machdep.cpu.brand_string)\n" | |
info+="Number of Cores:\n$(sysctl -n hw.ncpu)\n" | |
info+="Memory Information:\n$(sysctl hw.memsize)\n" | |
info+="GPU Information:\n$(system_profiler SPDisplaysDataType | grep 'Chipset Model:')\n" | |
info+="------------------------------\n" | |
fi | |
if command_exists df; then | |
info+="Disk Usage:\n$(df -h)\n" | |
info+="------------------------------\n" | |
fi | |
if command_exists lsblk; then | |
info+="Block Devices:\n$(lsblk)\n" | |
info+="------------------------------\n" | |
fi | |
if command_exists ip; then | |
info+="Network Configuration:\n$(ip addr)\n" | |
info+="------------------------------\n" | |
elif command_exists ifconfig; then | |
info+="Network Configuration:\n$(ifconfig)\n" | |
info+="------------------------------\n" | |
fi | |
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
if command_exists vcgencmd; then | |
info+="GPU Memory Allocation:\n$(vcgencmd get_mem gpu)\n" | |
info+="------------------------------\n" | |
fi | |
fi | |
echo -e "$info" | |
} | |
# Function to prompt for saving or displaying the report | |
function save_or_display() { | |
read -p "Do you want to save the report to a file or display on screen? (save/display) " answer | |
case ${answer} in | |
save ) | |
echo -e "$system_info" > system_report.txt | |
echo "Report saved as system_report.txt" | |
;; | |
display ) | |
echo -e "$system_info" | |
;; | |
* ) | |
echo "Invalid option. Exiting." | |
exit 1 | |
;; | |
esac | |
} | |
# Main script execution | |
system_info=$(gather_system_info) | |
save_or_display | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment