1. 程式人生 > >Ubuntu下獲取系統資訊shell指令碼

Ubuntu下獲取系統資訊shell指令碼

#!/bin/bash

# 獲取系統資訊
# 獲取cpu數量
get_cpu_num(){
    grep "cpu cores" /proc/cpuinfo | head -n1 | awk '{ print $NF }'
}
# 獲取總記憶體
get_memory_total(){
    free -h | awk 'NR==2{ print $2 }'
}
# 獲取可用記憶體
get_memory_free(){
    free -m | awk 'NR==2{ print $NF }'
}
# 獲取掛載到根目錄的檔案系統的總大小
get_disk_size(){
    df -h | grep -E "^.*/$" | awk '{ print $2 }'
}
# 獲取系統位數
get_system_bit(){
    getconf LONG_BIT
}
# 獲取當前系統正在執行的程序數
get_process(){
    ps aux | tail -n +2 | wc -l
}
# 獲取已安裝的軟體包數量
get_software_num(){
    dpkg -l | tail -n +6 | wc -l
}
# 獲取ip
get_ip(){
    ip a s | grep -w "inet" | awk 'NR==2{ print $2 }' | awk -F/ '{ print $1 }'
}

echo "cpu num: $(get_cpu_num)"
echo "memory total: $(get_memory_total)"
echo "memory free: $(get_memory_free)M"
echo "disk size: $(get_disk_size)"
echo "system bit: $(get_system_bit)"
echo "process: $(get_process)"
echo "software num: $(get_software_num)"
echo "ip: $(get_ip)"