shell

shell.1.3 bash特性:globbing与重定向

 

一、globbing , 文件通配
匹配模式:元字符

元字符:并不表示字符本身的意义,而是用来匹配一定范围内或指定符合条件的字符;

###################################

* : 匹配任意长度的任意字符;
? : 匹配任意单个字符;
[] : 匹配指定范围内的任意单个字符;

几种特殊格式:
[a-z] , [A-Z] : 表示所有英文字母中任意一个,不区分大小写;
[0-9] : 表示所有数字中的任意一个;
[a-z0-9] : 表示所有英文字母和数字中的任意一个(任意一个字母或数字),英文字母不区分大小写;
[abcdefg] : 表示括号范围内的一个字符;

[[:upper:]] : 表示任意一个大写字母;
[[:lower:]] : 表示任意一个小写字母;
[[:alpha:]] : 表示任意一个字母—任意一个大写字母或者小写字母;
[[:digit:]]   : 表示任意一个数字;
[[:alnum:]]: 表示任意一个字母(大写或者小写)或者数字;
[[:space:]] : 表示一个空白符号;
[[:punct:]] : 表示任意一个标点符号;

[^] : 匹配指定范围外的任意单个字符;

#######################################

eg . 理解如下匹配模式:
[^a-z] : 匹配大小写字母以外的任意字符;
[^0-9] : 匹配数字以外的任意单个字符;
[^[:upper:]] : 匹配大写字母以外的任意单个字符;
[^[:punct:]] : 匹配标点符号以外的任意单个字符;

显示 /var 目录下所有以 l 开头,以一个小写字母结尾,且中间出现一位任意字符的文件或目录;
ls -d /var/l?[[:lower:]]

显示 /etc 目录下以任意一个数字开头,且以非数字结尾的文件或目录;
ls -d /etc/[0-9]*[*0-9]
ls -d /etc/[[:digit:]]*[^[:digit:]]

显示 /etc 目录下以非字母开头,后面跟一个字母及其他任意长度任意字符的文件或目录;
ls -d /etc/[^a-z][a-z]*

复制 /etc 目录下所有以 m 开头,以非数字结尾的文件或目录至 /tmp/test 目录;
cp -r /etc/m*[^0-9] /tmp/test

显示 /usr/share/man 目录下所有以 man 开头,后跟一个数字结尾的文件或目录;
ls -d /usr/share/man[0-9]

复制 /etc 目录下所有以 .conf 结尾,且以 m,n,r,p 开头的文件或目录至 /tmp/abc 目录;
cp -r /etc/[m,n,r,p]*.conf /tmp/abc

二、IO重定向及管道
1、输入输出设备
可用于输入的设备:文件(键盘设备、文件系统上的常规文件、网卡等)
可用于输出的设备:文件(显示器、文件系统上的常规文件、网卡等)

2、程序的数据流有三种
输入的数据流:标准输入(stdin),键盘是标准的输入设备;
输出的数据流:标准输出(stdout),显示器是标准的输出设备;
错误输出流:错误输出(stderr),显示器是标准的错误输出设备;

3、文件描述符(文件的句柄): fd , file descriptor
标准输入: 0
标准输出: 1
错误输出: 2

4、IO重定向
输出重定向: >
特性:覆盖输出

追加输出重定向: >>
特性:追加输出

set命令:
set 是bash内嵌命令;设置或撤销shell选项的值或位置参数;
Set or unset values of shell options and positional parameters.

set -C : 开启功能,仅对当前shell有效,禁止覆盖输出重定向至已存在的文件;
#####此时如果想强制输出重定向至已存在的文件,则 >|

[root@localhost tmp]# ls
kkou_20171226
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue > /tmp/test.out
[root@localhost tmp]# 
[root@localhost tmp]# ls
kkou_20171226  test.out
[root@localhost tmp]# 
[root@localhost tmp]# set -C
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue > /tmp/test.out 
-bash: /tmp/test.out: cannot overwrite existing file
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue > /tmp/test2.out
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue > /tmp/test2.out 
-bash: /tmp/test2.out: cannot overwrite existing file
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue >| /tmp/test2.out 
[root@localhost tmp]#

set +C : 取消功能,仅对当前shell有效;

理解如下操作:
cat /etc/issue > /dev/tty1 重定向输出到tty1终端;
cat /etc/issue > /dev/sda 重定向输出到硬盘,文件会覆盖整个硬盘的数据!!!

错误输出流重定向: 2> , 2>>

[root@localhost tmp]# cat /etc/issueadfsdf > err.out
cat: /etc/issueadfsdf: No such file or directory
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issuesafsd 2> err.out 
[root@localhost tmp]# 

合并正常输出流和错误输出流:
第一种方式 : &> , &>>
第二种方式 : COMMAD > /path/to/somefile 2>&1
##########COMMAD >> /path/to/somefile 2>&1

特殊设备: /dev/null
举例:

[root@localhost ~]# 
[root@localhost ~]# head -2 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# 
[root@localhost ~]# echo $?
0
[root@localhost ~]# head -2 /etc/passwd &> /dev/null
[root@localhost ~]# 
[root@localhost ~]# head -2 /etc/passwd.txt &> /dev/null
[root@localhost ~]# 
[root@localhost ~]# echo $?
1
[root@localhost ~]#
[root@localhost tmp]# cat /etc/issue &> err2.out
[root@localhost tmp]# 
[root@localhost tmp]# more err2.out 
\S
Kernel \r on an \m

[root@localhost tmp]# cat /etc/isssdfsdfdsf &>> err2.out 
[root@localhost tmp]# 
[root@localhost tmp]# more err2.out 
\S
Kernel \r on an \m

cat: /etc/isssdfsdfdsf: No such file or directory
[root@localhost tmp]#
[root@localhost tmp]# cat /etc/issue > /tmp/abc.out 2>&1
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issuesfsdf >> /tmp/abc.out 2>&1
[root@localhost tmp]# 
[root@localhost tmp]# more abc.out 
\S
Kernel \r on an \m

cat: /etc/issuesfsdf: No such file or directory
[root@localhost tmp]#
#############   用  2<&1  也可以达到同样的效果;
[root@localhost tmp]# cat /etc/issue > /tmp/abc.out 2<&1
[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issuesfsdf >> /tmp/abc.out 2<&1
[root@localhost tmp]# 
[root@localhost tmp]# more abc.out 
\S
Kernel \r on an \m

cat: /etc/issuesfsdf: No such file or directory
[root@localhost tmp]#

输入重定向: <

tr 命令:转换或删除字符
tr – translate or delete characters
tr [OPTION]… SET1 [SET2]

功能描述:
把输入的数据中的字符,凡是在SET1中定义的范围内出现的,全部对位转换为SET2中出现的字符;

命令用法:
tr SET1 SET2 < /PATH/FROM/SOMEFILE 替换
tr -d SET1 < /PATH/FROM/SOMEFILE 删除
备注: 替换或者删除操作仅是提取数据流做处理,不影响源文件内容; SET1 和 SET2 要用单引号;

替换字符举例:

[root@localhost tmp]# tr [a-z] [A-Z]
hello word    #此处敲回车,小写字母对位替换为大写字母;
HELLO WORD    
who are you
WHO ARE YOU
^C
[root@localhost tmp]# tr [a-z] [A-Z] < /etc/issue
\S
KERNEL \R ON AN \M

[root@localhost tmp]# cat /etc/issue
\S
Kernel \r on an \m

[root@localhost tmp]#

删除符合条件的字符举例:

[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue
\S
Kernel \r on an \m

[root@localhost tmp]# 
[root@localhost tmp]# tr -d '[A-Z]' < /etc/issue
\
ernel \r on an \m

[root@localhost tmp]# tr -d '[[:upper:]]' < /etc/issue
\
ernel \r on an \m

[root@localhost tmp]#

此处创建文档,here document : <<
用法:
cat << EOF
cat > /PATH/TO/SOMEFILE << EOF
备注: 可以用其他字符代替EOF,但前后要呼应;

举例:

[root@localhost tmp]# 
[root@localhost tmp]# cat << EOF
> hello lucifer.
> what is up?
> EOF
hello lucifer.
what is up?
[root@localhost tmp]# cat > /tmp/aaa.out << EOF
> hello lucifer.
> what are you doing.
> EOF
[root@localhost tmp]# more aaa.out 
hello lucifer.
what are you doing.
[root@localhost tmp]#

管道:连接程序,实现将前一个命令的输出直接定向后一个程序当做输入数据流;
用法:
COMMAND | COMMAND2 | COMMAND3 | …

举例1:

[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/fstab | tr 'a-z' 'A-Z'

#
# /ETC/FSTAB
# CREATED BY ANACONDA ON WED DEC 27 05:42:43 2017
#
# ACCESSIBLE FILESYSTEMS, BY REFERENCE, ARE MAINTAINED UNDER '/DEV/DISK'
# SEE MAN PAGES FSTAB(5), FINDFS(8), MOUNT(8) AND/OR BLKID(8) FOR MORE INFO
#
/DEV/MAPPER/CENTOS-ROOT /                       XFS     DEFAULTS        0 0
UUID=78FBB254-BB6A-4847-8890-4410CCB77410 /BOOT                   XFS     DEFAULTS        0 0
/DEV/MAPPER/CENTOS-SWAP SWAP                    SWAP    DEFAULTS        0 0
[root@localhost tmp]# 
[root@localhost tmp]# who
root     tty1         2018-03-19 16:25
root     pts/0        2018-03-20 20:27 (192.168.206.1)
[root@localhost tmp]#

举例2:

[root@localhost tmp]# last
root     pts/0        192.168.206.1    Tue Mar 20 20:27   still logged in   
root     pts/0        192.168.206.1    Tue Mar 20 04:15 - 12:49  (08:33)    
root     pts/0        192.168.206.1    Mon Mar 19 16:27 - 00:39  (08:12)    
root     tty1                          Mon Mar 19 16:25   still logged in   
reboot   system boot  3.10.0-693.el7.x Mon Mar 19 16:24 - 00:20 (1+07:55)   
root     pts/0        192.168.199.111  Wed Dec 27 06:22 - 06:53  (00:31)    
root     tty1                          Wed Dec 27 05:49 - 06:55  (01:05)    
reboot   system boot  3.10.0-693.el7.x Wed Dec 27 05:48 - 06:55  (01:06)    

wtmp begins Wed Dec 27 05:48:58 2017
[root@localhost tmp]# 
[root@localhost tmp]# last | head -3 
root     pts/0        192.168.206.1    Tue Mar 20 20:27   still logged in   
root     pts/0        192.168.206.1    Tue Mar 20 04:15 - 12:49  (08:33)    
root     pts/0        192.168.206.1    Mon Mar 19 16:27 - 00:39  (08:12)    
[root@localhost tmp]# 
[root@localhost tmp]# last | head -3 | tr 'a-z' 'A-Z'
ROOT     PTS/0        192.168.206.1    TUE MAR 20 20:27   STILL LOGGED IN   
ROOT     PTS/0        192.168.206.1    TUE MAR 20 04:15 - 12:49  (08:33)    
ROOT     PTS/0        192.168.206.1    MON MAR 19 16:27 - 00:39  (08:12)    
[root@localhost tmp]# 
[root@localhost tmp]# last | head -3 | tr 'a-z' 'A-Z' | tr -d '0-9'
ROOT     PTS/        ...    TUE MAR  :   STILL LOGGED IN   
ROOT     PTS/        ...    TUE MAR  : - :  (:)    
ROOT     PTS/        ...    MON MAR  : - :  (:)    
[root@localhost tmp]#

tee 命令:
tee – read from standard input and write to standard output and files
从标准输入读出数据,写入标准输出,同时写入文件中;

tee [OPTION]… [FILE]…

用法:
COMMAND | tee /PATH/TO/SOMEFILE
COMMAND | tee /PATH/TO/SOMEFILE | COMMAND2 | …

举例:

tee 把前个命令的的执行做标准输出,同时写入指定文件中;

[root@localhost tmp]# 
[root@localhost tmp]# cat /etc/issue | tee /tmp/ab.out 
\S
Kernel \r on an \m

[root@localhost tmp]# more ab.out 
\S
Kernel \r on an \m

tee 把前个命令的执行结果写入指定文件中,并却把文件内容传递给下个命令;

[root@localhost tmp]# cat /etc/issue | tee /tmp/ab.out | tr 'a-z' 'A-Z'
\S
KERNEL \R ON AN \M

[root@localhost tmp]# more ab.out
\S
Kernel \r on an \m

[root@localhost tmp]#

把/etc/passwd文件的前六行信息转换为大写字符后输出;
head -n 6 /etc/passwd | tr ‘a-z’ ‘A-Z’

Leave a Reply

Your email address will not be published. Required fields are marked *