1.傳給指令碼一個引數:目錄,輸出該目錄中檔案最大的,檔名和檔案大小
#!/bin/bash if [ $# -ne 1 -o ! -d $1 ];then echo "Args is error." exit 3 fi LC=`du -a $1 | wc -l` for I in `seq $LC` ;do size=`du -a $1 | sort -nr | head -$I | tail -1 | awk '{print $1}'` filename=`du -a $1 | sort -nr | head -$I | tail -1 | awk '{print $2}'` if [ -f $filename ];then echo "The big file is $filename ,Size is $size." break fi done
2.查詢當前192.168.1.x網段內,那些IP被使用了,輸出這些IP到一個檔案中。
#!/bin/bash for I in {1..254};do N=`ping -c 1 192.168.1.$I | awk -F, '/received/ {print $2}' | cut -d' ' -f2` [ $N -eq 1 ] && echo "192.168.1.$I" >>/tmp/ips.txt done
3.把shell為bash的使用者輸出到/tmp/users.txt中
#!/bin/bash LC=`wc -l /etc/passwd | awk '{print $1}'` N=1 for I in `seq $LC` ;do SHE=`head -$I /etc/passwd | tail -1 | cut -d: -f7` U=`head -$I /etc/passwd | tail -1 | cut -d: -f1` if [ $SHE == '/bin/bash' ];then echo -e "$N \t $U" >> /tmp/users.txt N=$[$N+1] fi done
4.依次向/etc/passwd中的每個使用者問好,輸出使用者名稱和id,並統計一共有多少個使用者
(1)寫法一
#!/bin/bash file="/etc/passwd" LINES=`wc -l $file | cut -d" " -f1` for I in `seq 1 $LINES`;do userid=`head -$I $file | tail -1 |cut -d: -f3` username=`head -$I $file | tail -1 |cut -d: -f1` echo "hello $username,your UID is $userid" done echo "there are $LINES users"
(2).寫法二
#!/bin/bash file=/etc/passwd let num=0 for I in `cat $file`;do username=`echo "$I" | cut -d: -f1` userid=`echo "$I" | cut -d: -f3` echo "Hello,$username,your UID is $userid" num=$[$num+1] done echo "there are $num users"
5.切換工作目錄到/var ,依次向/var 目錄中每個檔案或子目錄問好(形如:Hello,log),並統計/var目錄下共有多少個檔案顯示出來
(提示:for File in /var/*;或 for File in 'ls /var ')
#!/bin/bash cd /var let num=0 for I in `ls /var/*`;do echo "hello $I" num=$[$num+1] done echo "the number of files is $num"
6.迴圈讀取檔案/etc/passwd的第2,4,6,10,13,15行,並顯示其內容,然後把這些行儲存到/tmp/mypasswd檔案中
#!/bin/bash file="/etc/passwd" for I in 2 4 6 10 13 15;do exec 3>/tmp/mypasswd line=`head -$I $file | tail -1` echo "$line" echo "$line" >&3 exec 3>&- done
7,寫一個指令碼
(1)顯示當前系統日期和時間,而後建立目錄/tmp/lstest
(2)切換工作目錄至/tmp/lstest
(3)建立目錄a1d,b56e,6test
(4)建立空檔案xy,x2y,732
(5)列出當前目錄下以a,x或者6開頭的檔案或目錄
(6)列出當前目錄下以字母開頭,後跟一個任意數字,而後跟任意長度字元的檔案或目錄
#!/bin/bash date mkdir -pv /tmp/lstest cd /tmp/lstest mkdir a1d b56e 6test touch xy x2y 732 ls [ax6]* ls [[:alpha:]][[:digit:]]*
8.新增10個使用者user1到user10,但要求只有使用者不存在的情況下才能新增
#!/bin/bash for I in `seq 1 10`;do cut -d: -f1 /etc/passwd |grep "user$I" 2>>/tmp/etc.err || useradd user$I done
9.通過ping命令測試192.168.0.151到192.168.0.254之間的所有主機是否線上
如果線上,就顯示“ip is up”
如果不線上,就顯示“ip is down”
#!/bin/bash for I in `seq 151 254`;do ping -c1 -w1 192.168.0.$I &>/dev/null && echo "192.168.0.$I is up" || echo "192.168.0.$I is down" done
10.zookeeper一個節點通過指令碼啟動所有的節點
#!/bin/bash if [ $# -ne 1 ] then echo "please give a parameter to tell me what to do for example [start|stop|restart|status]" && exit 1 fi echo "$1 zkServer in every node......" list=`cat /usr/local/zookeeper-3.4.6/conf/zoo.cfg | grep server | awk -F : '{print $1}' | awk -F '=' '{print $2}'` for i in $list do echo "$1 zkServer in $i......" ssh $i "/usr/local/zookeeper-3.4.6/bin/zkServer.sh $1" &> /dev/null done for i in $list do echo "show zkServer status in $i......" ssh $i "/usr/local/zookeeper-3.4.6/bin/zkServer.sh status" done