一、shell特殊符号

*  任意个任意字符

?  任意一个字符

#  注释字符

\  脱义字符

例如:

[root@localhost ~]# a=2

[root@localhost ~]# b=3

[root@localhost ~]# c=$a$b

[root@localhost ~]# echo $c

23

[root@localhost ~]# c=\$a\$b

[root@localhost ~]# echo $c

$a$b


管道符

cut 截取

-d  指定分隔符

-f  指定截取那一段

例如:

[root@localhost ~]# cat /etc/passwd |head -3 | cut -d ":" -f 1-3

root:x:0

bin:x:1

daemon:x:2


$变量的前缀

!$ 正则里面表示行尾


;多条命令写到一行,用;分割


~用户的家目录。正则表达式里表示匹配符


&把命令放到后台


> 正确重定向

>> 正确追加重定向

2> 错误重定向

2>>错误追加重定向

&>正确错误重定向


|| 用在shell中表示或者的意思,如果第一条命令执行成功,则不执行第二条命令。如果第一条命令不成功,则执行第二条命令

&& 当前面的命令执行成功时,才执行后面的命令

[root@localhost ~]# ls

1.txt  anaconda-ks.cfg

[root@localhost ~]# [ -d litongyao ] || mkdir litongyao   (没有litongyao这个目录,则执行第二条)

[root@localhost ~]# ls

1.txt  anaconda-ks.cfg  litongyao

[root@localhost ~]# [ -d litongyao ] && mkdir litongyao   (存在litongyao目录。在执行时会报错)

mkdir: 无法创建目录"litongyao": 文件已存在


二、 sort_wc_uniq命令


sort  排序

sort + 文件名     (默认是以“阿斯玛”排序)

特殊符号>数字>字母

-n            默认以数字去排序(默认字母和特殊符号为0,所以会排在最前面)

-r            反序排序



wc     统计

-l            统计行数

-m            统计字符数 (隐藏的换行符也算   用cat -A 查看隐藏符号)

-w            统计字符串 (默认以空白格或,为分隔符)

 

uniq    去重  (但是要先排好顺序,所以一般和sort一起用)

[root@localhost ~]# uniq 1.txt 

1

2

3

1

2

3

1111

2222

3333

111

[root@localhost ~]# sort 1.txt | uniq 

1

111

1111

2

2222

3

3333


 uniq -c       显示重复次数


[root@localhost ~]# sort 1.txt | uniq -c

      2 1

      1 111

      1 1111

      2 2

      1 2222

      2 3

      1 3333


三、tee_tr_split

tee 和输出重定>向有点像,但是把重定向的内容打印到屏幕上

-a 追加,和>>相似


tr  替换字符

例如:

[root@localhost ~]# echo "litongyao" |tr '[lty]' '[LTY]'

LiTongYao

[root@localhost ~]# echo "litongyao" |tr '[a-z]' '[A-Z]'

LITONGYAO


split 切割文件

split -b 100M + filename  以文件大小切割 (可以指定文件前缀,默认是x开头)

split -l 1000 + filename  以行数切割