zoukankan      html  css  js  c++  java
  • 老李分享:《Linux Shell脚本攻略》 要点(二)

    老李分享:《Linux Shell脚本攻略》 要点(二)

     

                poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。Linuxshell是测试开发工程师的基本功之一,所以在poptest测试开发课堂上加入了大量的linuxshell的课程,为了学员开发跨平台的测试平台打基础。

     

    1、cat

         cat -s //多个空白行压缩成一个 

         cat *.txt | tr -s ' '   //移除空白行

         cat -n //加行号

    2、find

    沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作。

    eg:

    find ./ ! -name "*.txt" -print

    [root@localhost program_test]# find ./  -type f -name "*.swp" -delete

    3、xargs 将标准输入数据转换成命令行参数。

    [root@localhost program_test]# cat out.txt | xargs  //将单行转化为多行

     

    [root@localhost program_test]# cat out.txt | xargs -n 3 //将单行转化为多行,每行的个数为3.

     

    //统计.c和.cpp的文件的代码行数.  xargs -0 将''作为定界符.

    [root@localhost program_test]# find . -type f -name "*.c*" -print0 | xargs -0 wc -l
      10 ./main/cos_value.c
      10 ./main/sin_value.c
       5 ./main/haha.c
      15 ./main/main.c
       8 ./hello.cpp
       8 ./sin.c
      32 ./review2.cpp
      24 ./review5.cpp
       7 ./hello.c
    119 total

     

    4.tr命令(translate的简写) 可对来自标准输入的字符进行替换、删除及压缩。

    即:将一组字符变为另一组字符。

    1)替换

    echo “HELLO” | tr [A-Z] [a-z]

    2)删除

    [root@localhost program_test]# echo "hello 123 world 456" | tr -d '0-9'

    hello  world 

    3)压缩字符

    [root@localhost program_test]# echo "GNU is     not UNIX" | tr -s ' '
    GNU is not UNIX

    //综合举例 

    [root@localhost program_test]# cat sum.txt
    1
    2
    3
    4
    5
    [root@localhost program_test]# cat sum.txt | echo $[ $(tr ' ' '+') 0 ]
    15

     

    5、md5校验

    [root@localhost program_test]# md5sum out.txt > out.txt.md5
    [root@localhost program_test]# cat out.txt.md5
    fd46d559bf0c90170fef3da3c3de4c67  out.txt

    //eg:

    [root@localhost program_test]# find ./ -type f -name "*.txt" -print0 | xargs -0 md5sum >> curr_dir.md5

    46e17910faf509df48dbfba9729ef018  ./banana.txt
    c1dbbf63209a5580c052dc557510e7fb  ./11.txt
    a80ddf02fa3a86c14066204e4bf2dbb9  ./multiline.txt

    [root@localhost program_test]# md5sum -c curr_dir.md5
    ./banana.txt: OK
    ./11.txt: OK
    ./multiline.txt: OK

     

    6、sort排序

    //sort 按第2列排序

    [root@localhost program_test]# sort -k 2 sort.txt
    4 bad 5000
    3  linux 50
    1   mac 2000
    2  winxp 100

    //sort 逆序排序
    [root@localhost program_test]# sort -r sort.txt
    4 bad 5000
    3  linux 50
    2  winxp 100
    1   mac 2000

     

    //综合举例:统计字符串中每个字符出现的次数

    [root@localhost program_test]# ./total_cnts.sh
    AHEBHAAA
    4A1B1E2H
    [root@localhost program_test]# cat total_cnts.sh
    INPUT="AHEBHAAA"
    output=$(echo $INPUT | sed 's/[^.]/& /g' | sed '/^$/d' | sort | uniq -c | tr -d ' ')
    echo $INPUT
    echo $output

     

    [释义]: sed 's/[^.]/& /g'    //任意一个字符后面都加上

    拆分如下:

    [root@localhost program_test]# input="ahebhaaa"
    [root@localhost program_test]# echo $input | sed 's/[^.]/& /g'
    a
    h
    e
    b
    h
    a
    a
    a

     

     sed '/^$/d'  //删除空行

    uniq -c         //统计各行文本出现的次数.

    tr -d '  '     //删除换行符以及空格字符

     

    7、临时文件命名

    [root@localhost program_test]# temp_file="/tmp/var.$$"
    [root@localhost program_test]# echo $temp_file
    /tmp/var.16565

  • 相关阅读:
    (大数 小数点) 大明A+B hdu1753
    (大数 万进制) N! hdu1042
    (next_permutation) 排列2 hdu 1716
    (set)产生冠军 hdu2094
    (Set) {A} + {B} hdu1412
    (set stringstream)单词数 hdu2072
    (set)MG loves gold hdu6019
    (set) 人见人爱A-B hdu2034
    (map)水果 hdu1263
    (map)What Are You Talking About hdu1075
  • 原文地址:https://www.cnblogs.com/poptest/p/4981550.html
Copyright © 2011-2022 走看看