1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名, IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。 腳本代碼 #!/bin/bash #author:wzc echo Hostname: `hostname` echo IP address:`ifconfig |head -n2 |tail -n1|cut -d. -f1-4 |cut -dt -f2|cut -dn -f1` echo System version: `cat /etc/system-release` echo kernel version: `uname -r` echo CPU Type:`lscpu | grep "Model name" |cut -d: -f2|tr -s " "` echo Disk size :`fdisk -l|grep "sda:"|sed 's/.*:'//|sed 's/,.*'// |sed 's/^[[:space:]]\+'//` echo Memory size : `free -m|grep "Mem"|tr -s [[:space:]] |cut -d" " -f 2` MB 腳本測試結果: Hostname: wzc.localdomain IP address: 10.1.253.22 System version: CentOS Linux release 7.2.1511 (Core) kernel version: 3.10.0-327.el7.x86_64 CPU Type: Intel(R) Core(TM) i5-4200M CPU @ 2.50GHz Disk size :128.8 GB Memory size : 1824 MB
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到 /root/etcYYYY-mm-dd中 腳本代碼 #!/bin/bash #author:wzc cp -a /etc /root/etc`date +%F` 腳本測試結果: [root@wzc date]# ./backup.sh [root@wzc date]# ls /root/etc2016-08-15/
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值 腳本代碼: #!/bin/bash #author:wzc echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 | sort -nr | head -1`%" 腳本測試結果 [root@wzc date]# ./disk.sh The max using rate is: 51%
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的 IPv4地址和連接數,并按連接數從大到小排序 腳本代碼 #!/bin/bash #author:wzc echo the links number is:`netstat -nt |tr -s ' ' |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr` 腳本測試結果 [root@wzc date]# ./link.sh the links number is: 1 10.1.250.38
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶 和第20用戶的ID之和 腳本代碼: #!/bin/bash #author:wzc number1=`cat /etc/passwd|sed -n "10p" |cut -d: -f3` number2=`cat /etc/passwd|sed -n "20p" |cut -d: -f3` let number=$number1+$number2 echo $number 腳本測試結果 [root@wzc date]# ./sumid.sh 70
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本, 計算這兩個文件中所有空白行之和 腳本代碼 #!/bin/bash #author:wzc file1=`grep "^$" $1 | wc -l` file2=`grep "^$" $2 | wc -l` file3=$[ $file1+$file2 ] echo "space is $file3個" 腳本測試結果 [root@wzc date]# ./sumspace.sh /etc/issue /etc/passwd space is 1個
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少 個一級子目錄和文件 腳本代碼 [root@wzc date]# vim sumfile.sh #!/bin/bash #author:wzc etcnum=`ls /etc/|wc -l` varnum=`ls /var/|wc -l` usrnum=`ls /usr/|wc -l` echo the totalfile is "$[$etcnum+$varnum+$usrnum]" 腳本測試結果 [root@wzc date]# ./sumfile.sh the totalfile is 293
8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數; 如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出; 如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數 腳本代碼 #!/bin/bash #author:wzc [[ $# -lt 1 ]] && echo "argnum less than 1" || grep -c '^[[:space:]]*$' $1 腳本測試結果 [root@wzc date]# ./argsnum.sh argnum less than 1
9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數, 測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”; 如果不可ping通,則提示用戶“該IP地址不可訪問” 腳本代碼 #!/bin/bash #author:wzc ping -c1 -W1 $1 &> /dev/null && echo ping successfull || echo ping failture 腳本測試結果 [root@wzc date]# ./hostping.sh ping failture
10、判斷硬盤的每個分區空間和inode的利用率是否大于80,如果是, 發郵件通知root磁盤滿 腳本代碼 #!/bin/bash #author:wzc dl=`df -hT|tr -s " "|cut -d" " -f6|grep -o "[[:digit:]]\+"|sort -nr | df -hT|tr -s " "|cut -d" " -f6|grep -o "[[:digit:]]\+"|sort -nr|sed -n '1p'` [ $dl -ge 80 ] && echo "Disk load is high, please timely manner!"|mail -s "Disk load warning" root 腳本測試結果 這里會發一封郵件給root,告知磁盤使用率過高,這是因為掛載光盤的原因
原創文章,作者:RecallWzc,如若轉載,請注明出處:http://www.www58058.com/35702