Linux(6)、漏斗家族&管道

漏斗家族

输出重定向

将正确的命令结果输入到文件中。

须知:echo -- display a line of text

>>

追加重定向,追加到最后一行。

[root@localhost fangqihan]# cat 1.txt 
haha
[root@localhost fangqihan]# echo 'hello linux' >> 1.txt 
[root@localhost fangqihan]# cat 1.txt 
haha
hello linux
2>>

只将错误的命令结果输入到文件中

[root@localhost fangqihan]# asas 'wewe' >> 1.txt 
-bash: asas: 未找到命令
[root@localhost fangqihan]# cat 1.txt 
haha
hello linux
[root@localhost fangqihan]# asas 'wewe' 2>> 1.txt 
[root@localhost fangqihan]# cat 1.txt 
haha
hello linux
-bash: asas: 未找到命令
&>>

不论命令正确与否,都将命令返回的结果输入到文件中。

[root@localhost fangqihan]# echo  'wewe' &>> 1.txt 
[root@localhost fangqihan]# cat 1.txt 
haha
hello linux
-bash: asas: 未找到命令
wewe

方法2:

commands >>3.txt 2>>3.txt
>

标准重定向,先清空,再写入内容。

[root@localhost fangqihan]# cat 1.txt 
a
b
c
d
[root@localhost fangqihan]# echo 'hello linux' > 1.txt 
[root@localhost fangqihan]# cat 1.txt 
hello linux

set -C     # 开启防止标准重定向功能,无法覆盖已存在的文件

    [root@localhost fangqihan]# echo 'haha' > 1.txt 
    -bash: 1.txt: 无法覆盖已存在的文件

set +C          # 关闭此功能

输入重定向

须知:tr -- translate or delete characters

<

从文件中获取输入

[root@localhost fangqihan]# cat 1.txt 
haha
hello linux
-bash: asas: 未找到命令
wewe

[root@localhost fangqihan]# tr 'a-z' 'A-Z' < 1.txt 
HAHA
HELLO LINUX
-BASH: ASAS: 未找到命令
WEWE
<<

<<:here document此处生成文档,cat >> 1.txt << EOF 追加多行。

[root@localhost ~]# cat 1.txt 
huck
END
[root@localhost ~]# cat >> 1.txt << EOF
> a
> b
> c
> EOF
[root@localhost ~]# cat 1.txt 
huck
END
a
b
c
[root@localhost ~]# 

管道 |

将管道前的命令的输出 当做 后一个命令的输入。

# example 1:对给定的字符串进行小写转换成大写字母
[root@localhost ~]# echo hello linux |tr a-z A-Z 
HELLO LINUX

# example 2:取出文件内容并将小写字母转换成大写展示出来
[root@localhost ~]# cat 1.txt 
huck
END
a
b
c
[root@localhost ~]# cat 1.txt | tr a-z A-Z
HUCK
END
A
B
C

# example 3:取出文件中的第3-4行的内容
[root@localhost ~]# cat -n 1.txt 
     1  huck
     2  END
     3  a
     4  b
     5  c
[root@localhost ~]# head -4 1.txt |tail -2
a
b

|tee:read from standard input and write to standard output and files

[root@localhost ~]# echo hi,my name is ham |tee 2.txt
hi,my name is ham

猜你喜欢

转载自www.cnblogs.com/fqh202/p/9434403.html