1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
#!/bin/bash
echo “the hostname is:`hostname`”
echo “the ip address is:`ifconfig | sed –n ‘2p’ |sed 's/^.*inet//'|sed 's/net.*//'`”
echo “the op is: `cat /etc/centos-release`”
echo “the kernel version is: `uname -r`”
echo “the cpu type is: `lscpu |sed -n '13p'|sed 's/^.*://'|tr -s ' '`”
echo “the memory size is: `free –m |sed -n '2p'|sed 's/^.*://'|tr -s ' '|cut -d" " -f2`MB”
echo “the disk size is:`fdisk -l|sed -n '2p'|sed 's/^.*://'|sed 's/\,.*//'|tr –d ' '`”
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
#!/bin/bash
cp –a /etc /root/etc`date +%F`
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
#!/bin/bash
echo “the lagerest disk partition is`df| grep 'sda'| tr -s ' ' ':'| cut -d: -f5 |sort |tail -1`”
4、編腳本/root/bin/links.sh,顯示正連接本主機每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
#!/bin/bash
netstat -nt |tr -s ' '|cut -d' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
#!/bin/bash
A10=`cat /etc/passwd|sed –n ‘10p’|cut –d: -f3`
A20=`cat /etc/passwd|sed –n ‘20p’|cut –d: -f3`
let sum=A10+A20
echo $sum
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
#!/bin/bash
A=`grep “^[[:space:]]*$” $1|wc -l`
B=`grep “^[[:space:]]*$” $2|wc -l`
let sum=A+B
echo $sum
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
#!/bin/bash
A=`ls –d /etc/* |wc -l`
B=`ls –d /var/* |wc -l`
C=`ls –d /usr/*|wc -l`
let sum=A+B+C
echo $sum
8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
#!/bin/bash
[[ $# -lt 1 ]] && echo "less one arg"||echo "`grep '^[[:space:]]*$' $1 |wc -l`"
9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
#!/bin/bash
ping -c1 -W1 $1 &> /dev/null && echo ping successfull || echo ping failture
10、判斷硬盤的每個分區空間和inode的利用率是否大于80,如果是,發郵件通知root磁盤滿
#!/bin/bash
maxc=`(df;df -i)|grep "sd"|tr -s ' ' |cut -d' ' -f5|grep -o "[[:digit:]]\+"|sort -nr|head -1`
[ $maxc -ge 80 ] && mail -s “diskwarning” root < ~/fi || exit
11、指定文件做為參數,判斷文件是否為.sh后綴,如果是,添加x權限
方法一:
#!/bin/bash
A=`echo $1 | grep -o "\.[^.]\+$"`
[[ "$A" == ".sh" ]] && chmod +x $1 || echo xxf
方法二:
#!/bin/bash
read -p "please input the file:" a
A1=`echo $a | grep -o "\.[^.]\+$"`
[[ "$A1" == ".sh" ]] && chmod +x $a || echo xxf
12、判斷輸入的IP是否為合法IP
#!/bin/bash
read -p "please input ip adress:" a
[[ $a =~
(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\> ]] && echo legal || echo ilegal
13、計算1+2+3+…+100
#!/bin/bash
a=`echo {1..100} |tr ' ' '+'`
let sum100=a
echo $sum100
14、輸入起始值A和最后值B,計算從A+(A+1)…+(B-1)+B的總和
#!/bin/bash
echo “the total count is:`seq + $1 $2 |bc`”
15、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/file1文件是否不可讀且不可寫
#!/bin/bash
[ ! -r /tmp/file1 -a ! -w /tmp/file1 ] && echo “`whoami`can’t rw” || echo “`whoami`can r or w”
[ ! \(-r /tmp/file1 -o -w /tmp/file1\) ] && echo “`whoami`can’t rw” || echo “`whoami`can r or w”
16、編寫腳本/root/bin/nologin.sh 和login.sh,實現禁止和允許普通用戶登錄系統
禁止普通用戶登錄:
#!/bin/bash
[ -e /etc/nologin ] && exit || touch /etc/nologin
echo "disable user login "
允許普通用戶登錄:
#!/bin/bash
[ -e /etc/nologin ] && rm -f /etc/nologin
echo "enable user login "
原創文章,作者:18612763863,如若轉載,請注明出處:http://www.www58058.com/35557