Chinaunix首页 | 论坛 | 博客
  • 博客访问: 560228
  • 博文数量: 252
  • 博客积分: 1068
  • 博客等级: 少尉
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-05 21:33
文章分类

全部博文(252)

文章存档

2013年(21)

2012年(231)

分类:

2012-06-26 19:00:00

原文地址:SHELL相关积累 作者:g_programming

目录

(一)     文件相关.. PAGEREF _Toc323326221 \h 1 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320031000000

1.     行内输入.. PAGEREF _Toc323326222 \h 1 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320032000000

2.     正则表达式中’[]’的特殊字符(都无特殊含义). PAGEREF _Toc323326223 \h 1 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320033000000

3.     执行字符串的内容.. PAGEREF _Toc323326224 \h 1 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320034000000

4.     默认显示前n.. PAGEREF _Toc323326225 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320035000000

5.     If [ “X$v1” == “X$v2” ];参数都应被””.. PAGEREF _Toc323326226 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320036000000

6.     #!/bin/bash 阻止shell设置IFS和PATH. PAGEREF _Toc323326227 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320037000000

7.     捕捉信号来处理.. PAGEREF _Toc323326228 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320038000000

8.     列举”a b c”和”1 2 3”的所有组合.. PAGEREF _Toc323326229 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200320039000000

9.     间接引用${!var}. PAGEREF _Toc323326230 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330030000000

10.       查看数组是否包含字符串str(正则匹配).. PAGEREF _Toc323326231 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330031000000

11.       sed/awk将变量传给shell可以用eval. PAGEREF _Toc323326232 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330032000000

12.       一个自动ftp的脚本(使用文件).. PAGEREF _Toc323326233 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330033000000

13.       自动实现telnet(使用管道).. PAGEREF _Toc323326234 \h 2 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330034000000

14.       重定向数据流失,被其他处理输入流的命令处理了.. PAGEREF _Toc323326235 \h 3 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330035000000

(二)     时间相关.. PAGEREF _Toc323326236 \h 3 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330036000000

1.     日期转星期.. PAGEREF _Toc323326237 \h 3 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330037000000

2.     日期转天数 (计算距离1970-01-01 00:00:00的天数).. PAGEREF _Toc323326238 \h 3 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330038000000

3.     天数转日期.. PAGEREF _Toc323326239 \h 3 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200330039000000

4.     日期转分钟.. PAGEREF _Toc323326240 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340030000000

5.     分钟转日期.. PAGEREF _Toc323326241 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340031000000

6.     日期转秒数.. PAGEREF _Toc323326242 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340032000000

7.     秒数转日期.. PAGEREF _Toc323326243 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340033000000

8.     日期转毫秒.. PAGEREF _Toc323326244 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340034000000

9.     毫秒转日期.. PAGEREF _Toc323326245 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340035000000

10.       判断8位数的日期是否合法.. PAGEREF _Toc323326246 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340036000000

(三)     目录相关.. PAGEREF _Toc323326247 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340037000000

1.     取文件夹名/root/home (/root/home/tt.txt). PAGEREF _Toc323326248 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340038000000

2.     获取文件名tt.txt (/root/home/tt.txt). PAGEREF _Toc323326249 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003300320036003200340039000000

 

 

cat << EOF this is a line EOF

[]*\.-]

str=’mkdir /root/tt’;eval $str

str=’mkdir /root/tt’ | echo $str | sh -x

sed ${n:-25}q

;参数都应被””

阻止shell设置IFSPATH

echo {a,b,c,e,f}{1,2,3}

${!varprefix*}间接引用

匹配之前所有以varprefix开头进行声明的变量

${!varprefix@}间接引用

匹配之前所有以varprefix开头进行声明的变量

 

if [[ "${array[@]}" =~ $str ]]  # 不匹配  ![[ "${array[@]}" =~ $str ]]

将变量传给shell可以用eval

eval $(awk 'BEGIN{print "a=ggg b=3"}')

然后

echo $a;echo $b

测试一下

#!/bin/sh

 

F="xxxx.ftp"

 

echo "open 192.168.7.51 21"     > $F

echo "user username passwd"     >> $F

echo "bin"                      >> $F

echo "cd /home/xxx/"            >> $F

echo "mput $1"                  >> $F

echo "bye"                      >> $F

 

ftp -i -in < $F

rm -rf $F

 

#!/bin/sh
( echo sybase
sleep 2
echo sybase
sleep 3
echo "\n"
sleep 2
echo "ps "
sleep 2
echo logout ) | telnet alpha

while read t1 t2 t3 t4

do

echo $t2 $t4

sed 's/.*/sed eat the line ( & )/'

done

 

15.    

 

function date2week {

    echo "$1 $2 $3" | awk '{

        z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;

        dow=(int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472629)%7;

        print dow

    }'

}

 

1970-01-01 00:00:00的天数)

1date +%s | awk '{print int($0/60/60/24)}'

2function 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

    }'

}

date2days `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

 

function 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)

    }'

}

days2date 14839

 

1date +%s | awk '{print int($0/60)}'

2function date2minutes {

    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;

        j=j*1440+$4*60+$5

        print j

    }'

}

date2minutes `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

 

function minutes2date {

    echo "$1" | awk '{

        i=$1; nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);

        a=i+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 %02d:%02d\n",yy,mm,dd,hh,nn)

    }'

}

minutes2date 21369299

 

1date +%s

2function date2seconds {

    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;

        j=j*86400+$4*3600+$5*60+$6

        print j

    }'

}

date2seconds `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

 

function seconds2date {

    echo "$1" | awk '{

        i=$1; ss=i%60; i=int(i/60); nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);

        a=i+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 %02d:%02d:%02d\n",yy,mm,dd,hh,nn,ss)

    }'

}

seconds2date 1282157959

 

1date +%s | awk '{print int($0*1000)}'

2function date2milliseconds {

    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;

        j=j*86400+$4*3600+$5*60+$6

        printf ("%d%s\n",j,$7)

    }'

}

date2milliseconds `echo "2010-08-18 18:59:19.073" | sed 's/-/ /g;s/:/ /g;s/\./ /g'`

 

function milliseconds2date {

    echo "$1" | awk '{

        i=$1; ms=i%1000; i=int(i/1000); ss=i%60; i=int(i/60); nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24);

 

i=int(i/24);

        a=i+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 %02d:%02d:%02d.%03d\n",yy,mm,dd,hh,nn,ss,ms)

    }'

}

milliseconds2date 1282157959073

 

1[[ `date -d'20101050' &>/dev/null` ]] && echo ok||echo no

2function 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

    }'

}

 

function 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)

    }'

}

 

num1=20105050

num2=20101001

arg1=`echo "$num1" | sed -r 's/(....)(..)(..)/\1 \2 \3/g'`

arg2=`echo "$num2" | sed -r 's/(....)(..)(..)/\1 \2 \3/g'`

days1=`date2days $arg1`

date1=`days2date $days1`

days2=`date2days $arg2`

date2=`days2date $days2`

[ "$num1" -eq "$date1" ] && echo "$num1 is valid date" || echo "$num1 is invalid date"

[ "$num2" -eq "$date2" ] && echo "$num2 is valid date" || echo "$num2 is invalid date"

 

11.    

12.    

 

${path%/*}

${path##/*/}

 

阅读(441) | 评论(0) | 转发(0) |
0

上一篇:c面试问题

下一篇:Linux内核Makefile浅析

给主人留下些什么吧!~~