- 论坛徽章:
- 50
|
本帖最后由 WilliBhamlll 于 2013-07-03 15:02 编辑
系统环境和软件版本如下:- $cat /proc/version
- Linux version 2.6.18-164.el5xen (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 16:06:30 EDT 2009
- $bash --version
- GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
- Copyright (C) 2005 Free Software Foundation, Inc.
- $awk --version|more -2
- GNU Awk 3.1.5
- Copyright (C) 1989, 1991-2005 Free Software Foundation.
- $sed --version|more -2
- GNU sed version 4.1.5
- Copyright (C) 2003 Free Software Foundation, Inc.
复制代码 第一题:- $ awk '$6{a[$6]++}END{for(i in a)print a[i],i|"sort -n|tail -10"}' illegal.log
- 16 91.237.249.67
- 19 74.91.23.27
- 22 222.186.26.26
- 23 111.73.45.13
- 27 142.4.117.83
- 28 220.250.58.172
- 29 220.250.52.90
- 30 220.250.58.171
- 36 220.250.58.170
- 70 220.250.52.66
复制代码 第二题:- awk -F '[ :]+' -vd=$(date +%I%M -d "-10 minutes") -vd2=$(LANG=C;date +%b) '/<Error>/&&$4$5>=d&&$1=="<"d2{system("mail -s "Log Error" admin@ChinaUnix.net")}' wls_ms1.out
复制代码 第三题:- #!/bin/bash
- dir=/opt/WebSphere/AppServer/profiles
- localip=192.168.1.2
- localdir=/var/ChinaUnix
- for((i=3;i<=100;i++))
- do
- if [ `date +%w` = 0 ];then
- ssh 192.168.1.$i "cd $dir;tar -g will -zcvf backup_full_192.168.1.$i_$(date +%F).tar.gz --exclude=./bin --exclude=./logs --exclude=./*.log --exclude=./*heapdump* --exclude=./*.gz --exclude=./*.tar --exclude=./zip --exclude=./bak .;scp backup_full_192.168.1.$i_$(date +%F).tar.gz root@$localip:$localdir"
- else
- ssh 192.168.1.$i "cd $dir;tar -g will -zcvf incremental_192.168.1.$i_$(date +%F).tar.gz --exclude=./bin --exclude=./logs --exclude=./*.log --exclude=./*heapdump* --exclude=./*.gz --exclude=./*.tar --exclude=./zip --exclude=./bak .;scp incremental_192.168.1.$i_$(date +%F).tar.gz root@$localip:$localdir"
- fi
- done
复制代码 第四题:
不同之处在于:
command1 && command2 || command3 可以有两个测试条件,if command1....无法实现。
如:- $ A=123
- $ [ -n "$A" ] && [ "$A" -lt 100 ] || echo 'too big!'
复制代码 第五题:- echo '1234
- > 1234567
- > 123456789'|awk -F '' '{s=0;for(i=1;i<=NF;i++){s+=$i};print s}'
- 10
- 28
- 45
复制代码 第六题:
1.- $ ./dd1.sh
- Enter your date:
- 20130523
- 143
- $cat dd1.sh
- #!/bin/bash
- #参考Shell_HAT版主代码
- #此脚本仅限于计算2013的某一天是这年的第几天。
- date2days(){
- echo "$*" | awk '{
- z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
- j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
- print j
- }'
- }
- echo "Enter your date:"
- read day1
- day2=20121231
- date1=$(date2days ${day1:0:4} ${day1:4:2} ${day1:6:2})
- date2=$(date2days ${day2:0:4} ${day2:4:2} ${day2:6:2})
- result=$(($date1-$date2))
- echo $result
复制代码 2.- $date +%F
- 2013-06-19
- $./dd2.sh
- 2013-06-18
- $cat dd2.sh
- #!/bin/bash
- date2days(){
- echo "$1 $2 $3" | awk '{
- z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
- j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
- print j
- }'
- }
- days2date(){
- echo "$1" | awk '{
- a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a;
- d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
- dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
- printf ("%4d-%02d-%02d\n",yy,mm,dd)
- }'
- }
- year=`date +%Y`; month=`date +%m`; day=`date +%d`
- days=`date2days $year $month $day`
- date=$(($days-1))
- days2date $date
复制代码 第七题:
第八题:
可以删除。rm *中的*是通配符,可以匹配0或多个任意字符。
例如a*b a与b之间可以有任意长度的任意字符, 也可以一个也没有, 如aabcb, a b, a012b, ab。
第九题:
举例如下:- $mkdir -p /tmp/1/2/{old,new,dist,bugs}
- $ls -R /tmp/1
- /tmp/1:
- 2
- /tmp/1/2:
- bugs dist new old
- /tmp/1/2/bugs:
- /tmp/1/2/dist:
- /tmp/1/2/new:
- /tmp/1/2/old:
复制代码 第十题:
1.- ls|grep -v '^20130605'
- ls|awk '!/20130605/'
- ls|sed '/20130605/!d'
复制代码 2.- ls|sed 's/.*/mv "&" \`echo & \| sed "s\/ \/\/g"\`/ge'
复制代码 3.- for i in `find dirA dirB -type f|awk -F'/' '{a[$NF]++}END{for(i in a)if(a[i]>=2)print i}'`;do rm -f dirA/$i;rm -f dirB/$i;done
复制代码 4.- for i in `find dirA dirB -type f|awk -F'[/.]' '{a[$(NF-1)]++}END{for(i in a)if(a[i]>=2)print i}'`;do rm do rm -f dirA/$i.che;rm -f dirB/$i.che;done
复制代码 |
|