1. 編寫shell腳本,計算1-100的和;
1 #! /bin/bash 2 sum=0 3 for i in `seq 1 100`; do 4 sum=$[$i+$sum] 5 done 6 echo $sum
2. 編寫shell腳本,要求輸入一個數字,然后計算出從1到輸入數字的和,要求,如果輸入的數字小於1,則重新輸入,直到輸入正確的數字為止;
1 #! /bin/bash 2 n=0 3 while [ $n -lt "1" ]; do 4 read -p "Please input a number, it must greater than "1":" n 5 done 6 7 sum=0 8 for i in `seq 1 $n`; do 9 sum=$[$i+$sum] 10 done 11 echo $sum 12 13
3. 編寫shell腳本,把/root/目錄下的所有目錄(只需要一級)拷貝到/tmp/目錄下;
1 #! /bin/bash 2 for f in `ls /root/`; do 3 if [ -d $f ] ; then 4 cp -r $f /tmp/ 5 fi 6 done
4. 編寫shell腳本,批量建立用戶user_00, user_01, … ,user_100並且所有用戶同屬於users組;
1 #! /bin/bash 2 groupadd users 3 for i in `seq 0 9`; do 4 useradd -g users user_0$i 5 done 6 7 for j in `seq 10 100`; do 8 useradd -g users user_$j 9 done
5. 編寫shell腳本,截取文件test.log中包含關鍵詞’abc’的行中的第一列(假設分隔符為”:”),然后把截取的數字排序(假設第一列為數字),然后打印出重復次數超過10次的列;
1 #! /bin/bash 2 awk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txt 3 sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt 4 awk '$1>10 {print $2}' /tmp/n2.txt
6. 編寫shell腳本,判斷輸入的IP是否正確(IP的規則是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。
1 #! /bin/bash 2 checkip() 3 { 4 if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then 5 a=`echo $1 | awk -F. '{print $1}'` 6 b=`echo $1 | awk -F. '{print $2}'` 7 c=`echo $1 | awk -F. '{print $3}'` 8 d=`echo $1 | awk -F. '{print $4}'` 9 10 fi 11 12 for n in $a $b $c $d; do 13 if [ $n -ge 255 ] || [ $n -le 0 ]; then 14 echo "the number of the IP should less than 255 and greate than 0" 15 return 2 16 else 17 echo "The IP you input is something wrong, the format is like 192.168.100.1" 18 return 1 19 fi 20 done 21 22 } 23 24 25 26 rs=1 27 while [ $rs -gt 0 ]; do 28 read -p "Please input the ip:" ip 29 checkip $ip 30 rs=`echo $?` 31 32 done 33 34 echo "The IP is right!" 35 判斷IP地址
7.編寫一個腳本,打印任何數的乘法表。如輸入3則打印
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
1 awk -vstr='3' 'BEGIN{for(i=1;i<=str;i++){for(p=1;p<=i;p++)printf p"*"i"="p*i"\t";printf "\n"}}'
8.編寫一個腳本,輸入自己的生日時間(YYYYMMDD),計算還有多少天多少個小時是自己的生日。
1 read -p "Input your birthday(YYYYmmdd):" date1 2 m=`date --date="$date1" +%m` 3 d=`date --date="$date1" +%d` 4 date_now=`date +%s` 5 y=`date +%Y` 6 7 birth=`date --date="$y$m$d" +%s` 8 internal=$(($birth-$date_now)) 9 if [ "$internal" -lt "0" ]; then 10 birth=`date --date="$(($y+1))$m$d" +%s` 11 internal=$(($birth-$date_now)) 12 fi 13 14 awk -vinternal=$internal 'BEGIN{d=int(internal/60/60/24);h=int((internal-24*60*60*d)/3600);print "There is : "d" days "h" hours."}'
9.編寫一個腳本,自動將用戶主目錄下所有小於5KB的文件打包成XX.tar.gz.(提示:用ls,grep,find等命令,文件一般指普通文件)
1 find ~ -size -5 -type f -maxdepth 1|xargs tar zcvpf backup.tar.gz
10.編寫一個程序,他的作用是先查看一下/root/test/logical這個名稱是否存在,若不存在,則創建一個文件。使用touch來創建,創建完成后離開;如果存在的話,判斷該名稱是否為文件,若為文件則將之刪除后新建一個目錄。文件名為loglical,之后離開;如果存在的話,而且該名稱為目錄,則刪除此目錄。
1 if [ ! -e "/root/test/logical" ]; then touch "hh"; elif [ -f "/root/test/logical" ];then rm /root/test/logical && mkdir logical&&exit;elif [ -d "/root/test/logical" ];then rm /root/test/logical; fi
11.導出 2013-05-24 15:00:00 ~ 2013-05-28 16:00:00 之間的apache訪問日志
1 sed -n '/24\/May\/2013:15:00:01/,/28\/May\/2013:16:59:58/p' xxxx-access_log > 20130524.15-20130528.16-access_log
PS:需要注意的是如果起始時間在日志中不存在,則整個截取將返回 0 行結果。而如果結束時間在日志中不存在,則會截取到日志的最后一條。所以在截取前得要找到最日志中最合適的起始點和結束點。
另一種做法是先使用grep去找到兩個點 再使用sed去截取
1 # 找出 2013-05-24 15點第一條記錄的時間[root@style logs]# grep '24/May/2013:15' xxxx-access_log | head -110.200.114.183 - - [24/May/2013:15:00:01 +0800] "GET /gp10/pic_259_218_1368781965.png HTTP/1.0" 401 484# 找出 2013-05-28 16點最后一條記錄的時間[root@style logs]# grep '28/May/2013:16' xxxx-access_log | tail -1222.92.115.195 - - [28/May/2013:16:59:58 +0800] "GET /favicon.ico HTTP/1.1" 404 17846# 然后取這兩個時間段之間的記錄
