1、編寫(xiě)腳本,統(tǒng)計(jì)/etc、/usr、/var目錄中有多少個(gè)一級(jí)子目錄和文件
#!/bin/bash
# danran
# time is Mon Jun 5 13:09:12 CST 2017
line1=`ls $1 | wc -l`
line2=`ls $2 | wc -l`
line3=`ls $3 | wc -l`
let sum=$line1+$line2+$line3
echo $sum
2、自動(dòng)生成腳本
#!/bin/bash
echo "#!/bin/bash
# filename $1
# author:danran
# time is `date +%F`" >$1
chmod +x $1
vim $1
3、編寫(xiě)腳本sumid.sh,計(jì)算/etc/passwd文件中的第10個(gè)用戶(hù)和第20個(gè)用戶(hù)的id之和
#! /bin/bash
user10="`head -n $2 $1 | tail -n 1|cut -d: -f3`"
user20="`head -n $3 $1 | tail -n 1|cut -d: -f3`"
let sum=$user10+$user20
echo "user id sum is $sum"
4、編寫(xiě)腳本/sumspace.sh,傳遞兩個(gè)文件路徑作為參數(shù)給腳本,計(jì)算這兩個(gè)文件中所有空白行之和
#!/bin/bash
spaceline1=`grep "^[[:space:]]*$" $1 | wc -l`
spaceline2=`grep "^[[:space:]]*$" $2 | wc -l`
echo "The sum of space line:$[spaceline1+spaceline2]"
5、如果用戶(hù)存在cmd1,將顯示用戶(hù)名存在 cmd2,如果用戶(hù)名不存在,將創(chuàng)建該用戶(hù)cmd3
id $user &>/dev/null && echo $user is exist || (useradd $user && echo "$user is created")
6、編寫(xiě)腳本createuser.sh,先判斷參數(shù)是否為一個(gè),如果參數(shù)個(gè)數(shù)不為1,提示usage:createuser.sh username,并退出腳本,返回為100的狀態(tài)碼
對(duì)用戶(hù)名判斷,如果存在,提示此用戶(hù)已存在,創(chuàng)建用戶(hù)并提示創(chuàng)建成功
#!/bin/bash
[ $# != 1 ] && echo "usage:createuser.sh username" && exit 100
id $1 &> /dev/null && echo "user is exits" ||( useradd $1 && echo "user is create")
7、編寫(xiě)腳本/root/bin/argsnum.sh,接受一個(gè)文件路徑作為參數(shù);如果參數(shù)個(gè)數(shù)小于1,則提示用戶(hù)“至少應(yīng)該給一個(gè)參數(shù)”,并立即退出;如果參數(shù)個(gè)數(shù)不小于1,則顯示第一個(gè)參數(shù)所指向的文件中的空白行數(shù)
#!/bin/bash
[ $# -lt 1 ] && (echo "please input a arge" && exit ) || echo "`grep "^$" $1 | wc -l`"
8、編寫(xiě)腳本/root/bin/hostping.sh,接受一個(gè)主機(jī)的IPv4地址做為參數(shù),測(cè)試是否可連通。如果能ping通,則提示用戶(hù)“該IP地址可訪問(wèn)”;如果不可ping通,則提示用戶(hù)“該IP地址不可訪問(wèn)”
#!/bin/bash
# filename hostping.sh
# author:danran
# time is 2017-06-06
echo $1 | egrep "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$" > /dev/null || { echo "danran" ; exit ; }
ping -c4 $1 &> /dev/null && echo "gai ip di zhi ke fangwen" ||echo "ip bu ke fangwen"
9、編寫(xiě)腳本/root/bin/checkdisk.sh,檢查磁盤(pán)分區(qū)空間和inode使用率,如果超過(guò)80%,就發(fā)廣播警告空間將滿(mǎn)
#!/bin/bash
# filename checkdisk.sh
# author:danran
# time is 2017-06-06
inode=`df -i | grep "^/dev/sd*" | tr -s ' ' '%'| cut -d'%' -f5| sort -r | head -n 1`
disk=`df | grep "^/dev/sd*" | tr -s ' ' '%'| cut -d'%' -f5| sort -r | head -n 1`
[ $inode -gt 80 ] && echo "danran"
[ $disk -gt 80 ] && echo "dan"
10、編寫(xiě)腳本/bin/per.sh,判斷當(dāng)前用戶(hù)對(duì)指定的參數(shù)文件,是否不可讀并且不可寫(xiě)
#!/bin/bash
# filename per.sh
# author:danran
# time is 2017-06-08
[ ! -r $1 -a ! -w $1 ] && echo "$1 file not read and not write"
11、編寫(xiě)腳本/root/bin/excute.sh ,判斷參數(shù)文件是否為sh后綴的普通文件,如果是,添加所有人可執(zhí)行權(quán)限,否則提示用戶(hù)非腳本文件
#!/bin/bash
# filename excute.sh
# author:danran
# time is 2017-06-08
[ $# == 0 ] && read -p "please input fimename" name || name=$1
[ -f $name ] && [[ "$name" =~ .sh$ ]] && chmod a+x $name || echo "$name not scripts file"