公眾微信號:http://blog.genesino.com
學習Linux命令,我們需要有一臺Linux服務器。有了服務器,就想看看它的性能怎樣。翻出自己較早前寫的一個腳本,一鍵查看系統大部分參數。
This is an old script used to display the hardware information of a server. Generated infos include hostname, IP, Bits-of-OS, CPU, memory, disk .etc.
#!/bin/bash
# -*- coding: UTF-8 -*-
#屏幕輸出
echo "This lists the information of this computer."
#輸出空行
echo
##tput setaf [0-7] –使用ANSI轉義設置前景色
#Color Code for tput:
#0 – Black
#1 – Red
#2 – Green
#3 – Yellow
#4 – Blue
#5 – Magenta
#6 – Cyan
#7 – White
##tput sgr0 – Turn off all attributes
##`hostname` 返回主機名
#`/sbin/ifconfig` ifconfig是linux中用于顯示或配置網絡設備(網絡接口卡)的命令
# sed -n '2p' 顯示文件的第2行
# cut是一個選取命令,就是將一段數據經過分析,取出我們想要的內容
# -d :自定義分隔符,默認為制表符。-f :與-d一起使用,指定顯示哪個區域。
echo "Hostname is $(tput setaf 3)`hostname`$(tput sgr0),\
Ip address is $(tput setaf 3)\
`/sbin/ifconfig | sed -n '2p' | cut -d ':' -f 2 | cut -d ' ' -f 1`.
$(tput sgr0)"
#---------------------------------------------------------------------------
#uname -a :顯示系統名、節點名稱、操作系統的發行版號、操作系統版本、運行系統的機器 ID 號。
# Linux ehbio 2.6.32-642.4.2.el6.x86_64 #1 SMP Tue Aug 23 19:58:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
#
nuclear=`uname -a | cut -d ' ' -f 3`
bitInfo=`uname -a | cut -d ' ' -f 12`
# if語句,判斷系統是64位還是32位
if test $bitInfo == "x86_64"; then
bit=64
else
bit=32
fi
#tput bold – Set bold mode
#head -n 打印每個文件的前n行,而不是打印默認的前10行
# /etc/issue 查看系統登陸信息、發行版本信息
#
echo "The $(tput bold)${bit}$(tput sgr0) bt operating \
system is $(tput bold) `head -n 1 /etc/issue`\
$(tput sgr0), Nuclear info is $(tput setaf 1)\
${nuclear}$(tput sgr0)."
#打印空行
echo
# `sed -n '5p' /proc/cpuinfo 得到如下結果model name : Intel(R) Xeon(R) CPU G7-4809 v2 @ 4.90GHz
#sed 's/[ ] */ /g'貌似什么也沒做啊,這是不對的,這句話是把多個相連空格變為單個空格
echo "The CPU is$(tput setaf 4)`sed -n '5p' /proc/cpuinfo \
| cut -d ':' -f 2 | sed 's/[ ] */ /g'`$(tput sgr0)."
echo
echo "There are $(tput setaf 5)\
`cat /proc/cpuinfo | grep "physical id" | sort | uniq \
| wc -l`$(tput sgr0) physical cpu, \
each physical \
cpu has$(tput setaf 5)`sed -n '12p' /proc/cpuinfo | \
cut -d ':' -f 2`$(tput sgr0) cores,\
$(tput setaf 5)`sed -n '10p' /proc/cpuinfo | \
cut -d ':' -f 2`$(tput sgr0) threads."
echo
echo "There are $(tput setaf 5)\
`cat /proc/cpuinfo | grep "cpu cores" | wc -l`$(tput sgr0) logical cpu."
# sed元字符集 ^ 匹配行開始,如:/^sed/匹配所有以sed開頭的行。* 匹配0個或多個字符,如:/ *sed/匹配所有模板是一個或多個空格后緊跟sed的行。
#sed 's/^ *//g' 刪除開頭的空格
#bc命令是一種支持任意精度的交互執行的計算器語言 bc -l 定義使用的標準數學庫
mem=`head -n 1 /proc/meminfo | cut -d ':' -f 2 | sed 's/^ *//g' | cut -d ' ' -f 1`
memInM=$(echo "$mem/1024/1024" | bc -l)
echo
echo "The memory of this server is $(tput setaf 5)${memInM}$(tput sgr0)G."
echo
echo "The disk information is :"
#linux中df命令的功能是用來檢查linux服務器的文件系統的磁盤空間占用情況。可以利用該命令來獲取硬盤被占用了多少空間,目前還剩下多少空間等信息。
#-h 方便閱讀方式顯示
echo "`df -h`"