shell学习笔记(一)

来源:互联网 发布:python 结巴分词 编辑:程序博客网 时间:2024/04/28 08:19

1.预定义变量

$#:位置参数的数量$*:所有位置参数的内容$?:命令执行后返回的状态$$:当前进程的进程号$!:后台运行的最后一个进程号$0:当前执行的进程名注:其中,“$?”用于检查上一个命令执行是否正确(在Linux中,命令退出状态为0表示该命令正确执行,任何非0值表示命令出错)。“$$”变量最常见的用途是用作临时文件的名字以保证临时文件不会重复。

2.find

格式:find pathname -options [-print -exec -ok] 命令的参数:
-path name find命令所查找的目录路径。
-print find命令将匹配的文件输出到标准输出。
-exec find命令对匹配的文件执行该参数所给出的s h e l l命令。相应命令的形式为’ c o m m -and’ {} \;,注意{ }和\;之间的空格。

Case1:批量修改文件名find . -name "guweihao*" -exec mv {} {}.bak \; Case2:为了在/ l o g s目录中查找更改时间在5日以前的文件并删除它们        find logs -type f -mtime+5 -exec rm{} \;

3.uniq

http://blog.51yip.com/shell/1022.html

[root@VM_4_251_centos zhangzhuo05]# sort file |uniq -d -c      2 i love tank        3 this is a test 

4.sort

http://roclinux.cn/?p=1350

-c 测试文件是否已经排序了-m 合并已经排序好的文件(不会再进行排序)-u 去重-n 按数值序排序-d 按字典序排序-k 指定要排序的域-o 输出到文件-r 逆序-t 指定分隔符
[root@VM_4_251_centos zhangzhuo05]# sort file |uniq -c|sort -n -r          3 this is a test        2 i love tank        1 you  have a try        1 whom have a try        1 we are good men        1 those are good men        1 t        1 i want to abroad        1 i am tank        1 WhoM have a try  [root@VM_4_251_centos zhangzhuo05]# sort file |uniq -c                 1 WhoM have a try        1 i am tank        2 i love tank        1 i want to abroad        1 t        3 this is a test        1 those are good men        1 we are good men        1 whom have a try        1 you  have a try  

5.cut

用来从标准输入或文本中剪切指定列或者域常用选项:-c:指定剪切字符数-f:field 指定剪切指定域数-d:delimit 指定除空格和tab外的域分隔符case:查看线上模块压力tail -f log/spuser.log|cut -d" " -f3|uniq -c[root@VM_4_251_centos zhangzhuo05]# cat file |cut -d" " -f3aatanktanktankaaahavetogoodgoodhahah

6.comm

http://man.linuxde.net/comm

7.od

查看二进制文件格式:Od –A<n> -j<num> -N<num> -t<format> -v –w<num> input_file-A 文件偏移的打印方式,我一般用-An-j<num> 跳过num个字节-N<num>  只读num个字节-t 数据输出格式,经常用-t d4-v 不用*替代重复的行-w<num> 每行打印的字节个数Case:od -An -v -t d4 -w4 data/blog_mon/0/indexdata.0di 其他常用格式:-t u4    -t x4

8.top,ps,pstree,pgrep

都是查看系统状态的命令,需要很熟悉我喜欢用的参数:top  shift + M : 看内存状态Ps  -axuwww  :查看所有程序Pstree –p   : 打印出程序pidPgrep –l    : 把程序名都打印出来
原创粉丝点击