Linux输入和输出

输入和输出

1 - 标准输入 默认是来自键盘的输入 stdin 0
2 - 标准输出 默认输出到终端窗口 stdout 1
3 - 标准错误输出 默认输出到终端窗口 stderr 2

I/O重定向

 1 > 覆盖
 2 
 3 - > 将标准输出重定向到文件中
 4 - 2> 将错误输出重定向到文件中
 5 - &> 将所有的输出都重定向到文件中
 6 
 7 禁止、允许覆盖
 8 
 9 - 禁止覆盖 set -C
10 - 允许覆盖 set +C
11 
12 >> 追加
13 
14 >> 将标准输出追加到文件中
15 2>> 将错误输出追加到文件中
16 &>> 将所有输出追加到文件中

标准输入和错误输入分开保存

1 ```
2 [root@localhost ~]#ls f 45yuio > log.log 2> error.log
3 [root@localhost ~]#cat log.log 
4 f
5 [root@localhost ~]#cat error.log 
6 ls: cannot access 45yuio: No such file or directory
7 
8 ```

合并所有的输出

1 - &> 覆盖重定向
2 - &>> 追加重定向
3 - command > file 2>&1
4 - command >> file 2>&1
5 - ():合并多个文件的输出
6 - /dev/null 黑洞

从文件导入stdin

tr 字符替换

  1 ```
  2 -t 截断
  3 -d 删除
  4 -s 压缩,去重
  5 -c 取反
  6 [root@localhost ~]#tr 'a-z' 'A-Z' < /etc/issue
  7 \S
  8 KERNEL \R ON AN \M
  9 [root@localhost ~]#tr 'a-z' 'A-Z' 
 10 qwertyy
 11 QWERTYY
 12 12345678
 13 12345678
 14 ASDFGHJ
 15 ASDFGHJ
 16 qwertyuio
 17 QWERTYUIO
 18 ^C
 19 [root@localhost ~]#tr ab 12
 20 ab
 21 12
 22 abb
 23 122
 24 asdfghjkl
 25 1sdfghjkl
 26 ^C
 27 [root@localhost ~]#tr abc 12
 28 ab
 29 12
 30 abc
 31 122
 32 abc
 33 122
 34 ^C
 35 [root@localhost ~]#tr ab 123
 36 ab
 37 12
 38 abb
 39 122
 40 avc
 41 1vc
 42 qbc
 43 q2c
 44 abc
 45 12c
 46 [root@localhost ~]#tr -t abc 12
 47 abc
 48 12c
 49 ab
 50 12
 51 [root@localhost ~]#tr -d abc
 52 qwertyui
 53 qwertyui
 54 an^H^H
 55 n
 56 
 57 abc
 58 
 59 artyibrtyuiocrtyuiop
 60 rtyirtyuiortyuiop
 61 ^C
 62 [root@localhost ~]#tr -d abc < /etc/issue
 63 \S
 64 Kernel \r on n \m
 65 
 66 [root@localhost ~]#cat /etc/issue
 67 \S
 68 Kernel \r on an \m
 69 [root@localhost ~]#tr -s a
 70 abc
 71 abc
 72 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc
 73 abc
 74 ^C
 75 [root@localhost ~]#tr -sc a 
 76 aasdaaaaaaa
 77 aasdaaaaaaa
 78 ^[[A^H^H^C
 79 [root@localhost ~]#tr -sc a 
 80 aaaaaaaaabbbbbbbbbbbbbccccccccddddddddddd
 81 aaaaaaaaabcd
 82 [root@localhost ~]#tr -dc a
 83 aaaaaaaaaaaabbbbbbbbbb
 84 asdfghjkqwertyuiozxcvbnmxcvbnm,.
 85 aaaaaaaaaaaaa
 86 ctrl+d 结束
 87 [root@localhost ~]#tr -dc "a\n"
 88 asdfghjk
 89 a
 90 wertyujk;l'
 91 
 92  
 93 
 94 asdfghj
 95 a
 96 [root@localhost test]#tr -d a < issue > issue 处理完成以后不能写会到源文件,要写到新的文件中
 97 [root@localhost test]#seq 1 10 > b
 98 [root@localhost test]#cat b
 99 1
100 2
101 3
102 4
103 5
104 6
105 7
106 8
107 9
108 10
109 [root@localhost test]#tr -d "\n" < b
110 12345678910[root@localhost test]#tr -d "\n" < b
111 [root@localhost test]#tr "\n" " " <b
112 1 2 3 4 5 6 7 8 9 10 [root@localhost test]#tr "\n" " " <b >c
113 [root@localhost test]#cat c
114 1 2 3 4 5 6 7 8 9 10 [root@localhost test]#tr " " "\n" <c
115 1
116 2
117 3
118 4
119 5
120 6
121 7
122 8
123 9
124 10
125 ```

多行发送给stdin

 1 ```
 2 # 第一种方式
 3 [root@localhost test]#cat > f1
 4 qwert
 5 wertyui
 6 wertyui
 7 wertyuiopasdfghjk
 8 sdfghjkl
 9 sdfyhjkl;sdfghjkl;xcvb
10 
11 # 第二种方式
12 [root@localhost test]#cat > f2 <<EOF
13 > qwerty
14 > qwertyu
15 > wertyui
16 > qwertyu
17 > EOF
18 EOF 不是必须得,只要两个相同就可以
19 ```

管道

1 管道使用“|”来表示
2 
3 命令1|命令2|命令3
4 
5 - 把命令1的输出结果当做命令2的输出结果,把命令2的输出结果当成命令3的输入结果
6 - 默认情况下,管道只能传送标准输出
7 - 如果需要把错误输出也传递,则需要|&
8 - 一般用来组合多个命令
9 - 有一些命令是不接受管道的
 1 ```
 2 [root@localhost test]#ls f1|tr 'a-z' 'A-Z'
 3 F1
 4 [root@localhost test]#ls f
 5 ls: cannot access f: No such file or directory
 6 [root@localhost test]#ls f|tr 'a-z' 'A-Z'
 7 ls: cannot access f: No such file or directory
 8 [root@localhost test]#ls f|&tr 'a-z' 'A-Z'
 9 LS: CANNOT ACCESS F: NO SUCH FILE OR DIRECTORY
10 [root@localhost test]#echo file{1..20}|touch 
11 touch: missing file operand
12 Try 'touch --help' for more information.
13 
14 ```
15 
16  

 

posted @ 2020-04-21 21:02  佛祖让我来巡山  阅读(1087)  评论(0编辑  收藏  举报

佛祖让我来巡山博客站 - 创建于 2018-08-15

开发工程师个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

Bootstrap中文网