1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。
echo "your host is `hostname` "
echo "ip address is `ifconfig | sed -n -r "2s@inet(.*)netmask(.*)@\1@p"`"
echo "`uname -r`"
echo "`uname -m`"
echo "`lscpu`"
echo "`free -m -h`"
echo "您的硬盤大小為 `df -h | sed -n '/sda/p' | tr -s " " | cut -d" " -f2 `"
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
read -p "please input backup directory: " backdir
read -p "please input destination directory: " desdir
[ -d $desdir ] && \cp -r $backdir $desdir/backup`date +%F ` || mkdir -p $/root/etc/
echo "backup successful!"
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
disk_use=`df |grep sd|tr -s " "|cut -d " " -f5|sed -n 's/%//p'|sort -rn|head -n 1`
echo $disk_use
unset disk_use
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
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之和
numbera=`sed -n '10p' /etc/passwd |cut -d: -f3`
numberb=`sed -n '20p' /etc/passwd |cut -d: -f3`
let suma_b=numbera+numberb
echo "user1 uig is $numbera"
echo "user2 uid is $numberb"
echo "two users id sum is $suma_b"
unset numberb suma_b numbera
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
read -p "Input first path of files: " first_path
read -p "Input second path of files:" second_path
a=`cat $first_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
b=`cat $second_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
let sum=a+b
echo "The total blank lines is: $sum"
unset a b sum
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
a=`ls -A /etc/|wc -l`
b=`ls -A ar/|wc -l`
c=`ls -A /usr/|wc -l`
let sumfile=a+b+c
echo "three directory have $sumfile files and directorys"
unset a b c sumfile
8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
[ $# -lt 1 ] && echo "At least one file" || echo "文件的空行數為 `grep "^$" $1 |wc -l` "
9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
[ ping $1 ] && echo "該IP地址可訪問" || echo “該IP地址不可訪問”
10、判斷硬盤的每個分區空間和inode的利用率是否大于80,如果是,發郵件通知root磁盤滿
[`df | grep "sda" | tr -s " "| cut -d " " -f5 | sed -n "s@%@@p" |head -n1` -gt 80 ] && echo "滿了"|mail -s "磁盤已滿" root
[`df -i | grep "sda" | tr -s " "| cut -d " " -f5 | sed -n "s@%@@p" |head -n1` -gt 80 ] && echo "滿了"|mail -s "磁盤已滿" root
11、指定文件做為參數,判斷文件是否為.sh后綴,如果是,添加x權限
read -p "請輸入文件file"
grep " .*\.sh" $file && chmod +x $file || exit 1
判斷輸入的ip是否為合法ip
兩種方法:
1.
read -p "請輸入您的ip" ip
echo '$ip' | grep -E -o '{((\<([1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\.\<([0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>) -o (0.0.0.0)}' && echo "該地址合格。" || echo "該地址不合格。"
2.
read -p "請輸入ip" ip
a=`echo "$ip" |sed -n -r "s@^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)@\1@p" `
b=`echo "$ip" |sed -n -r "s@^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)@\2@p" `
c=`echo "$ip" |sed -n -r "s@^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)@\3@p" `
d=`echo "$ip" |sed -n -r "s@^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)@\4@p" `
if [ $a -le 255 ] && [ $b -le 255 ] && [ $c -le 255 ] && [ $d -le 255 ] ; then
echo "該地址合格。"
else
echo "該地址不合格。"
fi
計算 1~100的值
本腳本計算1~A的值。
read -p 'what is A ' A
let a=($A+1)*$A/2
echo "$a"
輸入A 。B的值計算 A+ (A+1) …(B-1) +B 的總和判斷
read -p '請輸入數字A和B' A B
if [ $A -lt $B ] ; then
let a=($B+1)*$B/2
let b=($A-1)*$A/2
echo "和為"$(($a-$b))"."
else
if [ $A -gt $B ] ;then
let c=($A+1)*$A/2
let d=($B-1)*$B/2
echo "和為"$(($c-$d))"."
else
echo "和為$(($A*2))."
fi
fi
方法2:
read -p “請輸入數字A和B” A B
sum-=`seq -s "+" A B|bc`
echo "和為$sum"
簡單的計算器
echo $(($1$2$3))注意無提示,比較沒有技術含量。
三個簡單的課后作業,已完成,日后會上傳更多自己覺得有意義的腳本。
原創文章,作者:sjfbjs,如若轉載,請注明出處:http://www.www58058.com/33198