20180622 Linux shell 基础知识 8.7 — 8.13

20180622 Linux shell 基础知识 8.7 — 8.13
8.7 /8.8 shell 变量
8.9 环境变量配置文件
8.10 shell 特殊符 _cut 命令
8.11 sort_wc_uniq 命令
8.12 tee_tr_split 命令
8.13 shell 特殊符号
8.7 /8.8 shell 变量

变量

PATH,HOME,PWD,LOGNAME
 env命令
 set命令多了很多变量,并且包括用户自定义的变量
 自定义变量a=1
 变量名规则:字母、数字下划线,首位不能为数字
 变量值有特殊符号时需要用单引号括起来
 变量的累加
 全局变量export b=2
 unset变量
  1. env, 查看系统的变量
## 系统的变量都是大写的英文字母
[root@arron-02 ~]# env
XDG_SESSION_ID=7
HOSTNAME=arron-02
SELINUX_ROLE_REQUESTED=
TERM=xterm
.....

## 除了 UTF-8 外, 还有 GBK, GB2132 字体
LANG=zh_CN.UTF-8
  1. set , 不仅会查看系统的变量, 还会查看自定义的变量;
[root@arron-02 ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
......
SHLVL=1
SSH_CLIENT='192.168.122.1 53215 22'
SSH_CONNECTION='192.168.122.1 53215 192.168.122.132 22'
SSH_TTY=/dev/pts/0
......
  1. 自定义变量
[root@arron-02 ~]# a=111
[root@arron-02 ~]# echo $a
111
[root@arron-02 ~]# set |grep '111'
_=111
a=111


## 可以通过 set|less 查看到自定义的变量
[root@arron-02 ~]# set |less
.....
a=111
.....
  1. 变量规则, 系统的变量可以在配置文件中做更改, 但是一般不需要动它; 大写字母一般是系统的变量

变量名规则: 字母, 数字下划线, 首位不能为数字, 和 C 语言有点像

## 但是 _a1 一般不怎么用,用户体验不怎么好
[root@arron-02 ~]# a1_=2;a_1=3;_a1=4;echo $a1_;echo $a_1;echo $_a1
2
3
4

## 首位不能为数字
[root@arron-02 ~]# 1aa=5
-bash: 1aa=5: 未找到命令 

## 变量值有特殊符号时需要用单引号括起来
[root@arron-02 ~]# z=a b c
-bash: b: 未找到命令
[root@arron-02 ~]# echo $z

### 用单引号括起来, 使用时不需要脱意
[root@arron-02 ~]# z='a b c'
[root@arron-02 ~]# echo $z
a b c

### 用双引号括起来试试
[root@arron-02 ~]# y="d e f"; echo $y
d e f

### 但是双引号就无法实现脱意
[root@arron-02 ~]# x="a$yc"; echo $x
a

[root@arron-02 ~]# w='a$yc' ; echo $w
a$yc
  1. 变量的累加
[root@arron-02 ~]# d=6;e=7;echo $d$e
67
[root@arron-02 ~]# d='d$ec';echo $d$e
d$ec7
[root@arron-02 ~]# f="d$ec"; echo $f
d

## 多个字符时, 双引号中 $ec 被识别成了 $ec
[root@arron-02 ~]# f="d$ef" ; echo $f
d

## 多个字符时, 如果字母 f 隔开或者 $e 左右两边的都隔开, 就达到识别变量的效果

[root@arron-02 ~]# f="d$e"f;echo $f
d7f
[root@arron-02 ~]# f=d"$e"f; echo $f
d7f

## 多个字符时用单引号仅仅识别为一个符号, 没有识别为变量的前缀
[root@arron-02 ~]# f='d$ef'; echo $f
d$ef

  1. 全局变量, 指的是向下的全局, father shell --> son shell --> grandson shell , 而 grandson shell --> son shell --> father shell ;

command w 可以查看打开了多少个终端 command echo $SSH_TTY 可以查看当前在哪个远程终端下

## 查看打开的远程终端
[root@arron-02 ~]# w
 16:40:50 up  4:52,  4 users,  load average: 0.02, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.122.1    15:52    2.00s  0.23s  0.08s w
root     pts/1    192.168.122.1    12:12    3:40m  0.32s  0.32s -bash
root     pts/2    192.168.122.1    13:02    3:38m  0.05s  0.05s -bash
root     pts/3    192.168.122.1    16:40    4.00s  0.03s  0.03s -bash

## 在第一个远程终端下使用 echo $SSH_TTY
[root@arron-02 ~]# echo $SSH_TTY
/dev/pts/0

### 在复制远程终端下使用这个命令
[root@arron-02 ~]# echo $SSH_TTY
/dev/pts/3 

## 在一个终端下定义变量
[root@arron-02 ~]# arron=maxthinker;echo $arron
maxthinker

### 在另一个终端下查看这个变量, 发觉没有显示
[root@arron-02 ~]# echo $arron

## 在 bash 下查看, 打开一个 bash 相当于又打开了一个终端
[root@arron-02 ~]# bash

[root@arron-02 ~]# w
 16:49:12 up  5:00,  5 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      16:45    3:28   0.04s  0.04s -bash
root     pts/0    192.168.122.1    15:52    1:28   0.16s  0.16s -bash
root     pts/1    192.168.122.1    12:12    3:49m  0.32s  0.32s -bash
root     pts/2    192.168.122.1    13:02    3:46m  0.05s  0.05s -bash
root     pts/3    192.168.122.1    16:40    0.00s  0.16s  0.10s w

## 但还是显示在这个终端下
[root@arron-02 ~]# echo $SSH_TTY
/dev/pts/3

## 可以看到 pstree 在复制终端的子 shell 下
[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      └─sshd─┬─bash
        │             └─bash───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}

[root@arron-02 ~]# echo $arron

## 退出这个 bash , 发觉子 shell 已经没有了
[root@arron-02 ~]# exit
exit
[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      └─sshd─┬─bash
        │             └─bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}

## 在原终端下操作; 如上的演示说明这仅仅在非全局 / 本地的 shell 下生效
[root@arron-02 ~]# echo $arron
maxthinker

## 变成全局环境变量 export arron=maxthinker , 
[root@arron-02 ~]# export arron=maxthinker; bash
[root@arron-02 ~]# echo $arron
maxthinker

### 在下一个子 shell 下依然生效
[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      └─sshd─┬─bash───bash───bash───pstree
        │             └─bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@arron-02 ~]# echo $arron
maxthinker 

### 发觉在复制终端下依然不生效, 哪怕是重新重新开启的终端
[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      └─sshd─┬─bash───bash───bash
        │             ├─bash
        │             └─bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@arron-02 ~]# echo $arron

## grandson shell 里边设置的全局变量, 在 son shell 不生效, 所以全局是自上而下的, 不是自下而上的

[root@arron-02 ~]# export g=123;echo $g
123
[root@arron-02 ~]# exit
exit
[root@arron-02 ~]# echo $g
  1. unset , 取消变量

用法 : unset 变量名 , 直接打开终端在 bash 里运行就可以了

[root@arron-02 ~]# unset arron; echo $arron

## 在 grandson shell 里 取消的变量, 在 father shell 里边一样的会取消
[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      └─sshd─┬─bash───bash───bash
        │             ├─bash
        │             └─bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@arron-02 ~]# echo $arron

[root@arron-02 ~]# echo $arron

[root@arron-02 ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─auditd───{auditd}
        ├─chronyd
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───2*[bash]
        │      ├─sshd───bash───pstree
        │      └─sshd───bash
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─vmtoolsd───{vmtoolsd}
[root@arron-02 ~]# echo $arron
8.9 环境变量配置文件

环境变量配置文件

如果想要变量在 root 用户 和 其他用户下都生效, 可以放到 /etc/profile 下去

分系统层次和用户层次,

系统层次 : 包括 /etc/profile/etc/bashrc ; /etc/profile 和 /etc/bashrc 一般不要去编辑 , 遇到一些需要编辑时, 就去用户的家目录下编辑这些配置文件, ~/.bashrc , ~/.bash_profile ; 如果想要 root 用户 和 其他用户都生效, 可以放到 /etc/profile 下去

用户层次 : 包括 ~/.bashrc , ~/.bash_profile , ~/.bash_history , ~/.bash_logout

也可以这样理解, profile 和 bashrc, profile 是用户登录时会加载的 , bash 是系统执行 shell 脚本时会执行里边的配置

/etc/profile 用户环境变量,交互,登录才执行
 /etc/bashrc 用户不用登录,执行shell就生效
 ~/.bashrc
 ~/.bash_profile
 ~/.bash_history
 ~/.bash_logout
 PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
  1. 执行命令用的 source. 是一样的意思 , 加载配置文件

source /etc/profile 同理于 . /etc/profile

## 如下可看出 ~/.bash_profile 会自动调用 ~/.bashrc 
[root@arron-02 ~]# vim ~/.bash_profile
......
. ~/.bashrc
......

## ~/.bashrc 会调用 /etc/bashrc 
[root@arron-02 ~]# vim ~/.bashrc
......
. /etc/bashrc
.....
  1. 总的来说, 登录系统 ( 登录用户名和 port ) 就会调用 该用户的 ~/.bash_profile , ~/.bash_profile 会调用 ~/.bashrc , ~/.bashrc 会在执行 shell 脚本时执行里边的配置

  2. ~/.bash_logout , 如果想让用户退出时删除它的命令历史, 就可以将命令放在 ~/.bash_logout 里边

  3. PS 变量 可以在 /etc/bashrc 下查看到, 一般不会动它

[root@arron-02 ~]# , root 表示用户名, arron-02 表示主机名, ~ 表示目录 # 表示 登录的是 root 用户, $ 表示登录的是普通用户


[root@arron-02 ~]# vim /etc/bashrc
......
PS1="[\u@\h:\l \W]\\$ "
......

## ~ 示例如下
[root@arron-02 ~]# cd /etc/sysconfig/network-scripts/ ; pwd
/etc/sysconfig/network-scripts
[root@arron-02 network-scripts]#

4.1 PS1

[root@arron-02 network-scripts]# echo $PS1
[\u@\h \W]\$

## 将 \W 改为 \w 后, 路径变为了全局路径
[root@arron-02 network-scripts]# PS1='[\u@\h \w]\$ '
[root@arron-02 /etc/sysconfig/network-scripts]# echo $PS1
[\u@\h \w]\$ 

[rootarron-02 /tmp]# PS1='[\u@\h \w]\$ ' ; cd /tmp/
[root@arron-02 /tmp]#  

4.2 登录用户到终端叫 PS1, 在 Mysql 时会进入到另外一个小终端里, 会使用 PS2

root@arron-02:~# echo $PS2
>

## 示例如下
root@arron-02:~# for s in `seq 1 100`
> do 
> echo $s
> done
1
2
3
4
5
......

## 当然, PS2 如 PS1 一样, 可以做更改
root@arron-02:~# PS2="#"
root@arron-02:~# for s in `seq 1 10`
#do
#echo $s
#done
1
2
3
4
......

8.10 shell 特殊符 _cut 命令

特殊符号

* 任意个任意字符

 ? 任意一个字符

 # 注释字符

 \ 脱义字符

 | 管道符
  1. 脱意符号 \
root@arron-02:~# a=1;b=2;c=$a$b; echo $c;
12

## 如果想让 c=$a$b, 第一种方法是单引号, 第二种方法是用脱意符号 \
root@arron-02:~# c='$a$b'; echo $c
$a$b
root@arron-02:~# c=\$a\$b; echo $c
$a$b
  1. 管道符 |
root@arron-02:~# cat /etc/passwd | head -2 |cut -d ":" -f1
root
bin
root@arron-02:~# cat /etc/passwd | head -2 |cut -d ":" -f 1,3
root:0
bin:1
root@arron-02:~# cat /etc/passwd | head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1

## -c 指定字符
root@arron-02:~# cat /etc/passwd |head -2 |cut -c 4
t
:
8.11 sort_wc_uniq 命令

几个和管道有关的命令 , 这些命令不会对文件的内容做更改, 只会对源内容的输出内容做更改

cut 分割,-d 分隔符  -f 指定段号   -c 指定第几个字符 
 sort 排序, -n 以数字排序 -r 反序  -t 分隔符 -kn1/-kn1,n2
 wc -l 统计行数 -m 统计字符数 -w 统计词
 uniq 去重, -c统计行数
 tee 和>类似,重定向的同时还在屏幕显示
 tr 替换字符,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'
 split 切割,-b大小(默认单位字节),-l行数
  1. sort 往往和 uniq 结合着用

ASCII码采用7位二进制比特编码,可以表示128个字符。最小的ASCII码是0000000(二进制),最大的是1111111(二进制)。 ASCII码里边,符号在前,然后是0-9 , 然后是大写字母A-Z , 然后是小写字母a-z

## sort 是按 ASCII 排序
root@arron-02:~# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
arron:x:1000:1000::/home/arron:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
.......

## 更改 1.txt 且排序, 
root@arron-02:~# head /etc/passwd >> 1.txt; vim 1.txt

123
123
123
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
22222aaaaa
48888888asfdhasjk
*asdfjk
222111
223333
22aaa
<
>
{
]

## sort 排序, 小的都排在前面
root@arron-02:~# sort 1.txt


<
>
]
{
123
123
123
222111
22222aaaaa
223333
22aaa
48888888asfdhasjk
adm:x:3:4:adm:/var/adm:/sbin/nologin
*asdfjk
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync 

## -n 选项 sort -n , 字母被认为是 0, 字母和特殊符号被排在最前面, 然后是数字

root@arron-02:~# sort -n 1.txt


<
>
]
{
adm:x:3:4:adm:/var/adm:/sbin/nologin
*asdfjk
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
22aaa
123
123
123
22222aaaaa
222111
223333
48888888asfdhasjk

## -r 选线 sort -r 反序
root@arron-02:~# sort -nr 1.txt
48888888asfdhasjk
223333
222111
22222aaaaa
123
123
123
22aaa
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
*asdfjk
adm:x:3:4:adm:/var/adm:/sbin/nologin
{
]
>
<

2.1 忽略相同行使用-u选项或者uniq:详见链接: sort 用法

[root@mail text]# cat sort.txt
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5

[root@mail text]# sort -u sort.txt
aaa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5

或者

[root@mail text]# uniq sort.txt
aaa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5

2.2 -t 以段为分隔符, sort的-n、-r、-k、-t选项的使用

[root@mail text]# cat sort.txt
AAA:BB:CC
aaa:30:1.6
ccc:50:3.3
ddd:20:4.2
bbb:10:2.5
eee:40:5.4
eee:60:5.1

#将BB列按照数字从小到大顺序排列:
[root@mail text]# sort -nk 2 -t: sort.txt
AAA:BB:CC
bbb:10:2.5
ddd:20:4.2
aaa:30:1.6
eee:40:5.4
ccc:50:3.3
eee:60:5.1

#将CC列数字从大到小顺序排列:
[root@mail text]# sort -nrk 3 -t: sort.txt
eee:40:5.4
eee:60:5.1
ddd:20:4.2
ccc:50:3.3
bbb:10:2.5
aaa:30:1.6
AAA:BB:CC

# -n是按照数字大小排序,-r是以相反顺序,-k是指定需要爱排序的栏位,-t指定栏位分隔符为冒号


## -k 的用法

FStart.CStart Modifie,FEnd.CEnd Modifier
-------Start--------,-------End--------
 FStart.CStart 选项  ,  FEnd.CEnd 选项

### 从公司英文名称的第二个字母开始进行排序:
$ sort -t ' ' -k 1.2 facebook.txt
baidu 100 5000
sohu 100 4500
google 110 5000
guge 50 3000

** 解释: 使用了-k 1.2,表示对第一个域的第二个字符开始到本域的最后一个字符为止的字符串进行排序。你会发现baidu因为第二个字母是a而名列榜首。sohu和 google第二个字符都是o,但sohu的h在google的o前面,所以两者分别排在第二和第三。guge只能屈居第四了

### 只针对公司英文名称的第二个字母进行排序,如果相同的按照员工工资进行降序排序

$ sort -t ' ' -k 1.2,1.2 -nrk 3,3 facebook.txt
baidu 100 5000
google 110 5000
sohu 100 4500
guge 50 3000

** 解释: 由于只对第二个字母进行排序,所以我们使用了-k 1.2,1.2的表示方式,表示我们“只”对第二个字母进行排序。(如果你问“我使用-k 1.2怎么不行?”,当然不行,因为你省略了End部分,这就意味着你将对从第二个字母起到本域最后一个字符为止的字符串进行排序)。对于员工工资进行排 序,我们也使用了-k 3,3,这是最准确的表述,表示我们“只”对本域进行排序,因为如果你省略了后面的3,就变成了我们“对第3个域开始到最后一个域位置的内容进行排序” 了
  1. wc 统计个数 , 不管有多少选项, 当多个选项在一起时, 输出的内容就会从小到大, 从左往右排列; wc -w 只会以空格来确定单词的数量

wc -l 统计行数 wc -m 统计字符数 wc -w 统计词

root@arron-02:~# wc -l 1.txt; wc -m 1.txt
25 1.txt
464 1.txt
root@arron-02:~# wc -w 1.txt
23 1.txt

## 写一个文档 2.txt
root@arron-02:~# vim 2.txt

123
abc

root@arron-02:~# cat -A 2.txt
123$
abc$

## 2.txt 有 8 个字符, 其实加上结束符还有换行符
root@arron-02:~# wc -m 2.txt
8 2.txt

## 在 wc 命令中, 不管顺序如果, 
root@arron-02:~# wc -lmw 2.txt
2 2 8 2.txt
root@arron-02:~# wc -wml 2.txt
2 2 8 2.txt

### 更改 2.txt
root@arron-02:~# vim 2.txt

123
abc def ghi

## 如下, 不管有多少选项,  当多个选项在一起时, 输出的内容就会从小到大, 从左往右排列


root@arron-02:~# wc -wml 2.txt
 2  4 16 2.txt

## 更改 2.txt
root@arron-02:~# vim 2.txt
123
abc def ghi,jkl
123
abc
1
1
2

root@arron-02:~# wc -w 2.txt
9 2.txt
  1. uniq 去重 , 要先排序再去重
## 更改 2.txt 如下
root@arron-02:~# vim 2.txt

123
abc def ghi
123
abc
1
2
1

## 去重, 发觉没有变化
root@arron-02:~# uniq 2.txt
123
abc def ghi
123
abc
1
2
1

## sort -u 2.txt , 奏效
root@arron-02:~# sort -u 2.txt
1
123
2
abc
abc def ghi 

## 将 2.txt 做更改, 更改为如下
root@arron-02:~# vim 2.txt

123
abc def ghi
123
abc
1
1
2

## 再去重, 里边的内容 1 已经被去重了, 所以 uniq 需要先排序 - sort 
root@arron-02:~# uniq 2.txt
123
abc def ghi
123
abc
1
2

uniq -c 统计重复行数
root@arron-02:~# sort 2.txt|uniq -c
      2 1
      2 123
      1 2
      1 abc
      1 abc def ghi

8.12 tee_tr_split 命令
tee 和>类似,重定向的同时还在屏幕显示
 tr 替换字符,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'
 split 切割,-b大小(默认单位字节),-l行数
  1. tee ; > 重定向内容到后边的文件, 但是不会在屏幕上显示; 而 tee 会有显示; tee -a 追加
root@arron-02:~# sort 2.txt|uniq -c > a.txt ; cat a.txt
2 1
2 123
1 2
1 abc
1 abc def ghi,jkl      

## 使用 tee 命令去重定向
root@arron-02:~# sort 2.txt|uniq -c |tee a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc def ghi,jkl

## 清空内容 >filename 
root@arron-02:~# >a.txt ; cat a.txt
root@arron-02:~# 

## tee -a 追加
root@arron-02:~# sort 2.txt|uniq -c |tee -a a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc def ghi,jkl
root@arron-02:~# sort 2.txt|uniq -c |tee -a a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc def ghi,jkl
root@arron-02:~# sort 2.txt|uniq -c |tee -a a.txt
      2 1
      2 123
      1 2
      1 abc
      1 abc def ghi,jkl 

## 查看文件内容
root@arron-02:~# cat -n a.txt
     1	      2 1
     2	      2 123
     3	      1 2
     4	      1 abc
     5	      1 abc def ghi,jkl
     6	      2 1
     7	      2 123
     8	      1 2
     9	      1 abc
    10	      1 abc def ghi,jkl
    11	      2 1
    12	      2 123
    13	      1 2
    14	      1 abc
    15	      1 abc def ghi,jkl 

  1. tr 命令 , 替换命令
root@arron-02:~# echo "arronlinux" |tr '(al)' '(AL)'
ArronLinux

## 小写全部换成大写
root@arron-02:~# echo "arronlinux" |tr '[a-z]' '[A-Z]'
ARRONLINUX

## 只能替换为一个数字, 不能切换为多个数字
root@arron-02:~# echo "arronlinux" |tr '[a-z]' '[0-9]'
0]]]]]8]]]
root@arron-02:~# echo "arronlinux" |tr '[a-z]' '1'
1111111111
  1. split 切割;

日志内容很大, 需要做切割 split 切割,-b大小(默认单位字节),-l行数

## split 切割示例
root@arron-02:~# find /etc/ -type f -name "*conf" |xargs  cat  >> a.txt ; du -sh a.txt
256K	a.txt

root@arron-02:~/test# split -b 1000 a.txt ; du -sh a.txt
392K	a.txt
root@arron-02:~/test# ls
a.txt  xat  xbn  xch  xdb  xdv  xep  xfj  xgd  xgx  xhr  xil  xjf  xjz  xkt  xln  xmh  xnb  xnv  xop  xpj
xaa    xau  xbo  xci  xdc  xdw  xeq  xfk  xge  xgy  xhs  xim  
...

## 不给定单位, 默认会是 b , 切割后的名称会从xaa 到xaz  
root@arron-02:~/test# du -sb *
401031	a.txt
1000	xaa
1000	xab
1000	xac
1000	xad
...

### 当 xyz 完之后, 就从 xzaaa 开始
xyz 
xzaaa

root@arron-02:~/test# /bin/rm -f x*; ls
a.txt

root@arron-02:~/test# split -b 100k a.txt; du -sh *
392K	a.txt
100K	xaa
100K	xab
100K	xac
92K	xad

## 指定切割后的名称
root@arron-02:~/test# split -b 100k a.txt abc. ; ls 
abc.aa  abc.ab  abc.ac  abc.ad  a.txt  xaa  xab  xac  xad

root@arron-02:~/test# split -b 100k a.txt abc. ; ls 
abc.aa  abc.ab  abc.ac  abc.ad  a.txt  xaa  xab  xac  xad

## 按行切割 -l 
root@arron-02:~/test# split -l 1000 a.txt; ls -l * ; wc -l *
-rw-r--r--. 1 root root 401031 6月  24 23:49 a.txt
-rw-r--r--. 1 root root  37223 6月  25 00:21 xaa
-rw-r--r--. 1 root root  40225 6月  25 00:21 xab
-rw-r--r--. 1 root root  34554 6月  25 00:21 xac
-rw-r--r--. 1 root root  30453 6月  25 00:21 xad
-rw-r--r--. 1 root root  44681 6月  25 00:21 xae
-rw-r--r--. 1 root root  34275 6月  25 00:21 xaf
-rw-r--r--. 1 root root  33939 6月  25 00:21 xag
-rw-r--r--. 1 root root  34248 6月  25 00:21 xah
-rw-r--r--. 1 root root  42290 6月  25 00:21 xai
-rw-r--r--. 1 root root  34990 6月  25 00:21 xaj
-rw-r--r--. 1 root root  33705 6月  25 00:21 xak
-rw-r--r--. 1 root root    448 6月  25 00:21 xal
 11019 a.txt
  1000 xaa
  1000 xab
  1000 xac
  1000 xad
  1000 xae
  1000 xaf
  1000 xag
  1000 xah
  1000 xai
  1000 xaj
  1000 xak
    19 xal
 22038 总用量
8.13 shell 特殊符号

特殊符号

$ 变量前缀,!$组合,正则里面表示行尾
 ;多条命令写到一行,用分号分割
 ~ 用户家目录,后面正则表达式表示匹配符
 & 放到命令后面,会把命令丢到后台
 > >> 2> 2>> &>
 [ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc]
 || 和 && ,用于命令之间
  1. ; 示例
## ; 示例
root@arron-02:~/test# for s in `seq 1 10`; do echo $s; done

root@arron-02:~# ls -l 1.txt; wc -l 2.txt
-rw-r--r--. 1 root root 464 6月  24 21:56 1.txt
7 2.txt
  1. || , 两条命令是或者的关系, 有一条执行就行; && , 前面的命令成功了后边的才执行, 前面的命令执行不成功, 后边的也不执行
## || 示例
root@arron-02:~# ls 1.txt || wc -l 2.txt
1.txt

root@arron-02:~# ls
1.txt      2.txt    2.txt.bak  4.txt    anaconda-ks.cfg  test   xyz.txt  z.txt
1.txt.bak  2.txt.1  3.txt      abc.txt  peter.txt        X.txt  Y.txt

## [ -d arronlinux ] , 一个文件存在且是目录, 
root@arron-02:~# [ -d arronlinux ] || mkdir arronlinux
root@arron-02:~# ls 
1.txt      2.txt    2.txt.bak  4.txt    anaconda-ks.cfg  peter.txt  X.txt    Y.txt
1.txt.bak  2.txt.1  3.txt      abc.txt  arronlinux       test       xyz.txt  z.txt

root@arron-02:~# [ -d arronlinux ] || mkdir arronlinux

## && 示例
root@arron-02:~# ls 
1.txt      2.txt    2.txt.bak  4.txt    anaconda-ks.cfg  peter.txt  X.txt    Y.txt
1.txt.bak  2.txt.1  3.txt      abc.txt  arronlinux       test       xyz.txt  z.txt
root@arron-02:~# [ -d arronlinux ] && mkdir arronlinux
mkdir: 无法创建目录"arronlinux": 文件已存在 

转载于:https://my.oschina.net/u/3869385/blog/1834891

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值