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

 

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

1、cat

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

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

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 将'\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 '\n' '+') 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/[^.]/&\n/g' | sed '/^$/d' | sort | uniq -c | tr -d ' \n')
echo $INPUT
echo $output

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

拆分如下:

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

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

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

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

7、临时文件命名

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

老李分享:《Linux Shell脚本攻略》 要点(二)的更多相关文章

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

    老李分享:<Linux Shell脚本攻略> 要点(八)   1.打印进程 [root@localhost program_test]# ps -e | head  PID TTY     ...

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

    老李分享:<Linux Shell脚本攻略> 要点(七)   1.显示给定文件夹下的文件的磁盘适用情况 [root@localhost program_test]# du -a -h ./ ...

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

    老李分享:<Linux Shell脚本攻略> 要点(六)   1.打印网络接口列表 [root@localhost touch_more]# ifconfig | cut -c-10 | ...

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

    老李分享:<Linux Shell脚本攻略> 要点(五)   //1.打包.解包 [root@localhost program_test]# tar -cf output.tar 11. ...

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

    老李分享:<Linux Shell脚本攻略> 要点(四)   1.IP地址的正则表达式: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} 2. ...

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

    老李分享:<Linux Shell脚本攻略> 要点(三)   1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...

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

    老李分享:<Linux Shell脚本攻略> 要点(一)   第一章:Shell起步基础 1.变量:在bash中,每一个变量的值都是字符串.无论你给变量赋值时,有没有使用引号,值都会以字符 ...

  8. 读《Linux Shell脚本攻略》(第2版) 总结

    前段时间读完了<Linux Shell脚本攻略>(第2版)这本书,给部分想读这本书的人分享下个人感受. 说下这本书的难度吧.纯新手或者只懂少部分编程知识的人,读起来还是有很大难度的.以我为 ...

  9. 读《Linux Shell脚本攻略》(第2版) 一遍、二遍体会

    前段时间读完了<Linux Shell脚本攻略>(第2版)这本书,给部分想读这本书的人分享下个人感受. 第一遍体会解读:就像黑夜中的灯塔,指明前进的道路. 推荐指数:强烈推荐. 书中讲解的 ...

随机推荐

  1. 解决项目中找不到Maven Dependencies

    项目中找不到Maven Dependencies 正常的Maven项目应该是这样的 自己的项目中却没有Maven Dependencies,自己百度了, 发现解决不了,最后发现在.classpath和 ...

  2. Tomcat使用Memcached Session Manager管理Session

    Tomcat使用Memcached Session Manager管理Session 废话不多说,直接进入主题.项目使用阿里云负载均衡+ECS服务器集群进行部署,Tomcat使用8.5版本.阿里云负载 ...

  3. Zookeeper + Kafka 集群搭建

    第一步:准备 1. 操作系统 CentOS-7-x86_64-Everything-1511 2. 安装包 kafka_2.12-0.10.2.0.tgz zookeeper-3.4.9.tar.gz ...

  4. jquery的ajax提交后,会跳转页面

    今天在写代码的时候,遇到一个很奇怪的问题,一个form表单,用的是ajax的方式提交.结果,在服务器端php中,使用exit(),函数后都不能停止,并且继续跳转到本页.请求如下: 第一个请求中,其实我 ...

  5. 手把手教你webpack、react和node.js环境配置(下篇)

    上篇我介绍了前端下webpack和react.redux等环境的配置,这篇将继续重点介绍后台node.js的配置. 这里是上篇链接:手把手教你webpack.react和node.js环境配置(上篇) ...

  6. java 使用Stack来判断Valid Parentheses

    假如定义形如"{}[]()"或者"{[()]}"的模式为valid,"[{]"或者"(("的模式为invalid,那么我 ...

  7. StringBuffer与StringBuilder的区别,及实现原理

    区别 1.StringBuffer 与 StringBuilder 中的方法和功能完全是等价的, 2.只是StringBuffer 中的方法大都采用了 synchronized 关键字进行修饰,因此是 ...

  8. Python 3 集合基础和概念!

    Python 3 集合基础和概念! Python 3中,集合是无序的,所以不能进行切片和索引操作. 创建集合有两个方法:set()方法创建的集合是可变的,可被迭代的:frozenset()方法创建的集 ...

  9. 消息队列-ActiveMQ

    1 业务需求描述 举例描述: 再警情通报的业务时通过发送消息界面可以选择 警情联络,和船情通报两种消息 发送方式可分为 一对一发送:部门对部门.个人对个人 一对多发送:部门对多部门.个人对多人 2 功 ...

  10. Octave Tutorial(《Machine Learning》)之第一课《数据表示和存储》

    Octave Tutorial 第一课 Computation&Operation 数据表示和存储 1.简单的四则运算,布尔运算,赋值运算(a && b,a || b,xor( ...