Bash基础(2)

    通配符  组合键  数据重定向  管道  tee命令

 

文本名“通配符”

*:匹配任意长度的任意字符;

?:匹配任意单个字符;

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

[0-9]

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

[^a-b]

 

字符集合:

 [:lower:] 匹配任何小写字母

 [:upper:] 匹配任何大写字母

 [:alnum:] 匹配任何字母

 [:digit:]  匹配任何数字

 [:space:] 匹配空格符

 [:punct:] 匹配任何标点符号

 [:alnum:] 匹配任何字母

 示例

#显示/etc/下文件名刚好是3个的文件名
 [roger@oc3137372501 ~]$ ll -d /etc/???
drwxr-xr-x.  7 root root 4096 Jul 24 09:59 /etc/gdm
drwxr-xr-x.  3 root root 4096 Jun 19  2014 /etc/hal
drwxr-xr-x.  4 root root 4096 Feb 25  2014 /etc/ibm
drwxr-xr-x.  2 root root 4096 May 27  2010 /etc/jvm
drwxr-xr-x.  5 root root 4096 May 25 09:13 /etc/kde
drwxr-xr-x.  6 root root 4096 Jul 24 10:00 /etc/lvm
drwxr-xr-x.  3 root root 4096 Jul 24 10:03 /etc/ntp
drwxr-xr-x.  4 root root 4096 Feb 25  2014 /etc/opt
drwxr-xr-x. 10 root root 4096 Feb 25  2014 /etc/pki
 
#显示/etc/下以ss开头的文件名
[roger@oc3137372501 ~]$ ll -d /etc/ss*
drwxr-xr-x. 2 root root 4096 Jul 24 09:57 /etc/ssh
drwxr-xr-x. 2 root root 4096 Jul 24 09:40 /etc/ssl
 
#显示/etc/下文件名中含有数字0-3的文件名
[roger@oc3137372501 ~]$ ll -d /etc/*[0-3]*
drwxr-xr-x. 4 root root 4096 Jul 24 09:38 /etc/dbus-1
-rw-r--r--. 1 root root 5139 Jul 16  2014 /etc/DIR_COLORS.256color
drwxr-xr-x. 3 root root 4096 May 13  2010 /etc/gnome-vfs-2.0
drwxr-xr-x. 2 root root 4096 May  7 17:24 /etc/gtk-2.0
drwxr-xr-x. 2 root root 4096 Jul 24 09:56 /etc/iproute2


组合键

  Ctrl+l:清屏

  Ctrl+a: 切换至命令行首

  Ctrl+e:切换至命令行尾

  Ctrl+c:取消命令执行

  Ctrl+m:就是回车

  Ctrl+s:暂停屏幕输出

  Ctrl+q:恢复屏幕输出

  Ctrl+z: 暂停目前命令

  Ctrl+u:删除光标所在处至行首的内容;

  Ctrl+k: 删除光标所在处至行尾的内容;

 

数据流重定向

  程序:指令+数据

读入数据:input 

输出数据:output 

  

  标准输入(stdin): keyboard 代码为0,使用<<<  /dev/stdin

  标准输出(stdout): monitor 代码为1,使用>>>  /dev/stdout

  标准错误输出(stderr): monitor 代码为2,使用2> 2>>  /dev/stderr

  

 输出重定向:

    COMMAND > NEW_POS, COMMAND >> NEW_POS

覆盖的方式输出

>> 追加的方式输出

 

 set -C 开启如果覆盖重定向目标文件存在,则禁止执行;

 set +C 关闭如果覆盖重定向目标文件存在,则禁止执行;

 

 同时重定向标准输出流和错误输出流:

 

#输出到不同的文件
 COMMAND > /path/to/file.out 2> /path/to/file.err
  [roger@oc3137372501 ~]$ rm / >test/right_file 2>test/err_file
 #输出到同一文件夹
 COMMAND > /path/to/file.out 2>&1
  [roger@oc3137372501 ~]$ rm / >test/test_file 2>&1
 COMMAND &> /path/to/file.out
 [roger@oc3137372501 ~]$ rm / &>test/out_file


 

输入重定向:

<

用文件内容来代替键盘输入

[roger@oc3137372501 test]$ cat > catfile < err_file 
[roger@oc3137372501 test]$ cat catfile 
rm: cannot remove `/': Is a directory


<< 代表结束输入的意思

Here Document<<

cat >> /path/to/somefile << EOF

 

tr 命令:用来从标准输入中通过替换或删除操作进行字符转换

语法:tr -d -s ["string1_to_translate_from"]["string2_to_translate_to"]

参数:

-d 删除信息中string1字符串

-s 替换重复字符串

#替换字符串
[roger@oc3137372501 test]$ cat catfile 
how are you ?
how old are you?
aaa
ttt
ccc
kkk
[roger@oc3137372501 test]$ tr ttt 111 < catfile 
how are you ?
how old are you?
aaa
111
ccc
kkk
#删除信息中a字符串
[roger@oc3137372501 test]$ tr -d a < catfile 
how re you ?
how old re you?
 
ttt
ccc
Kkk
#替换重复字符串 替换连续重复字符
[roger@oc3137372501 test]$ tr -s t t < catfile 
how are you ?
how old are you?
aaa
t
ccc
kkk


4 管道命令pipe:

 COMMAND1 | COMMAND2 | COMMAND3...

示例:

[roger@oc3137372501 test]$ ls -al /etc | more
total 2492
drwxr-xr-x. 136 root root    12288 Aug 25 17:26 .
dr-xr-xr-x.  28 root root     4096 Aug 25 09:14 ..
drwxr-xr-x.   4 root root     4096 Feb 25  2014 acpi
-rw-r--r--.   1 root root       46 Aug 24 18:56 adjtime.....


管道命令仅会处理standard output,对于standard error output 会忽略

管道命令必须能够接收来自前一个命令的数据成为standard input继续处理才行


5tee命令
读取标准输入,把这些内容同时输出到标准输出和(多个)文件中。
语法:tee [OPTION]... [FILE]...
参数:
  -a: 追加输出,不覆盖
  -i: 忽略中断
示例:

# tee 只是标准输出 后边没有参数
[roger@oc3137372501 test]$ tee < catfile 
how are you ?
how old are you?
aaa
ttt
ccc
Kkk

#tee - 两次标准输出
[roger@oc3137372501 test]$ tee - < catfile 
how are you ?
how old are you?
aaa
ttt
ccc
kkk
how are you ?
how old are you?
aaa
ttt
ccc
Kkk

# tee file 标准输出的同时,输出到file 如果file不存在 则创建文件
[roger@oc3137372501 test]$ cat catfile | tee test_tree
how are you ?
how old are you?
aaa
ttt
ccc
Kkk
[roger@oc3137372501 test]$ cat test_tree 
how are you ?
how old are you?
aaa
ttt
ccc
kkk

# tee -a file 不覆盖文件,追加输出到文件
[roger@oc3137372501 test]$ cat catfile | tee -a test_tree
how are you ?
how old are you?
aaa
ttt
ccc
kkk
[roger@oc3137372501 test]$ cat test_tree 
how are you ?
how old are you?
aaa
ttt
ccc
kkk
how are you ?
how old are you?
aaa
ttt
ccc
Kkk

# tee file1 file2...输出到多个文件
[roger@oc3137372501 test]$ cat catfile | tee file1 file2 
how are you ?
how old are you?
aaa
ttt
ccc
kkk


如有错误,敬请指正!

谢谢!