linux的IO重定向管道

在proc目录中存放的都是系统当前状态信息,比如内核,进程运行的状态信息(每个进程的进程号都在其中),这些信息并不会占用磁盘空间,在开机关机时会清空。
其中有fd目录,这个目录存放的是文件的描述信息或 Process I/O channels。

输出重定向:
">"重定向,linux的重定向并不是web中的那个,而是将输出的信息重新定义输出到别的地方,是操作文件内容的方法。两次重定向到同一个文件的时候,后一次会将前一次抹去只保留新的一次,所以也叫覆盖。

“>>”追加,和覆盖不同的是,追加并不会将之前的信息抹去而是在内容之后填写新的内容。所以叫追加。

“<”输入重定向,示输入的流并不是从键盘或者什么地方输入而是直接从文件中输入获取。

举个栗子:

[root@localhost test-gsc]# ping 172.16.12.1 > 1.txt 
^C
[root@localhost test-gsc]# cat 1.txt
PING 172.16.12.1 (172.16.12.1) 56(84) bytes of data.
64 bytes from 172.16.12.1: icmp_seq=1 ttl=64 time=0.489 ms
64 bytes from 172.16.12.1: icmp_seq=2 ttl=64 time=0.692 ms
64 bytes from 172.16.12.1: icmp_seq=3 ttl=64 time=2.35 ms

--- 172.16.12.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.489/1.177/2.350/0.833 ms
[root@localhost test-gsc]# echo shshjhs > 1.txt 
[root@localhost test-gsc]# cat 1.txt 
shshjhs
[root@localhost test-gsc]# echo jaldhfa >> 1.txt 
[root@localhost test-gsc]# cat 1.txt 
shshjhs
jaldhfa

将ping 的结果重定向到1.TXT文件中,cat文件能够看到ping 的结果信息。
但是第二次将echo的结果重定向到1.TXT中再次cat发现重定向之前的ping信息已经被覆盖了,只剩下新的信息。
而追加后再次cat,发现保留了前一次重定向时写入的信息。

输出的结果又正确和错误之分。">" “>>” “<” 这样书写的话参数默认为1和0 。也就是说“>”等价于“1>” ">>“等价于"1>>”,参数默认为1,“<”等价于“0<”,这个参数默认为0。

[root@localhost test-gsc]# ls ladsfjhalwfjh test > 1.txt 
ls: cannot access ladsfjhalwfjh: No such file or directory
[root@localhost test-gsc]# cat 1.txt
test:
dir1
dir2
dir3

重定向后发现,没有文件的错误信息并没有进入1.TXT文件而是在下方输出了。因为默认的写法不能将错误的输出信息重定向到1.TXT文件中。test是目录所以显示了目录内容的信息。

用如下方法 : 2>

[root@localhost test-gsc]# ls dfaflj 2> 1.txt
[root@localhost test-gsc]# cat 1.txt 
ls: cannot access dfaflj: No such file or directory
[root@localhost test-gsc]# 

这样就可以将错误信息重定向到文件中了。

正确的信息和错误的信息重定向到同一个文件用&>。

[root@localhost test-gsc]# ls aifgsakfha 1.txt &> aaa.txt 
[root@localhost test-gsc]# cat aaa.txt 
ls: cannot access aifgsakfha: No such file or directory
1.txt

输出信息的转化:2>&1 将错误的输出转化为正确的输出。这样就可以输出到正确的文件中了。注:错误的输出不能通过管道,用这个就可以解决的。

[root@localhost test-gsc]# ls alfijakisdhyfa > aaa.txt 2>&1
[root@localhost test-gsc]# cat aaa.txt 
ls: cannot access alfijakisdhyfa: No such file or directory

间输出重定向到黑洞文件

[root@localhost test-gsc]# ls easkifi &> /dev/null 

null黑洞文件是一个永远都填不满的文件,并且进入其中的会消失。

输入重定向:
"<"等价于“0<”,从文件输出。
mail邮件发送给g,邮件内容为“arfds af”。这个是用键盘随意输入的。

[root@localhost test-gsc]# mail -s "ssssss" g
arfds
af
^C
(Interrupt -- one more to kill letter)
EOT

登录g用户
看标注的几行,收到了标题为ssss的邮件内容。

[g@localhost ~]$ cat /var/spool/mail/g 

From root@localhost.localdomain  Sat Oct 19 09:58:12 2019
Return-Path: <root@localhost.localdomain>
X-Original-To: g
Delivered-To: g@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
	id DDDF82103FB3; Sat, 19 Oct 2019 09:58:11 +0800 (CST)
Date: Sat, 19 Oct 2019 09:58:11 +0800
To: g@localhost.localdomain
Subject: ssssss  **********************************
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20191019015811.DDDF82103FB3@localhost.localdomain>
From: root@localhost.localdomain (root)
Status: O

arfds   *********************************************
af   *********************************************

下面用输入重定向。
没有键盘输入的时候,语句直接结束

[root@localhost test-gsc]# mail -s "test01" g < /etc/hosts
[root@localhost test-gsc]# 

把文件hosts的内容作为邮件内容发给g
登录g 看标注的几行,依旧能收到内容。

From root@localhost.localdomain  Sat Oct 19 10:00:56 2019
Return-Path: <root@localhost.localdomain>
X-Original-To: g
Delivered-To: g@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
	id 0F80C2103FB3; Sat, 19 Oct 2019 10:00:56 +0800 (CST)
Date: Sat, 19 Oct 2019 10:00:55 +0800
To: g@localhost.localdomain
Subject: test01   ***********************************
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20191019020056.0F80C2103FB3@localhost.localdomain>
From: root@localhost.localdomain (root)
Status: O
***********************************
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
***********************************

管道:
管道这个名字十分贴切,符号为“|”,作用是将一个程序的输出结果输送给另一个程序的输入。

管道遵循着先进先出的原则。

linux万事皆文件,管道也是个文件,一种特殊的文件,是7中文件类型之一,p表示管道文件。
1、管道文件有大小限制,是一个固定大小的缓冲区。
2、管道是半双工的,一端读,另一端只能写。
3、双方共用一个管道是,一端进行操作后,必须等另一端操作完成才能释放管道内容,进行下一步操作。
4、管道文件只能被使用一次,用后即焚。。。。。。
可以用mkfifo创建管道文件。

[root@localhost sanshell]# ll guandaofile 
prw-r--r--. 1 root root 0 Oct 19 10:46 guandaofile
[root@localhost sanshell]# 
[root@localhost sanshell]# mkfifo guandaofile
[root@localhost sanshell]# echo 234 > guandaofile 
  //此时如果没有人接收这内容那么会一直等待,也不能进入下面的操作。

//在另一个控制台打开管道文件,收到内容。

[root@localhost ~]# tail /tmp/test-gsc/sanshell/guandaofile 
234   

这时第一个控制台才被释放。

[root@localhost sanshell]# 

因为是阅后即焚的,所以>>追加也没有用,怎么都只能看到一条数据。

管道只接受上一个指令的正确输出 错误输出不会接收

参数传递xargs
并不是所有文件都能通过管道的。

[root@localhost xargs]# cat xargses.txt 
/tmp/test-gsc/sanshell/file1
/tmp/test-gsc/sanshell/file2
/tmp/test-gsc/sanshell/file3
/tmp/test-gsc/sanshell/file4
/tmp/test-gsc/sanshell/file5
[root@localhost xargs]# cat xargses.txt | ls 
xargses.txt
[root@localhost xargs]# 

file12345这些文件因为没有接口接收不到。

[root@localhost xargs]# cat xargses.txt | xargs ls -l
ls: cannot access /tmp/test-gsc/sanshell/file1: No such file or directory
-rw-r--r--. 1 root root 0 Oct 18 11:19 /tmp/test-gsc/sanshell/file2
-rw-r--r--. 1 root root 0 Oct 18 11:19 /tmp/test-gsc/sanshell/file3
-rw-r--r--. 1 root root 0 Oct 18 11:19 /tmp/test-gsc/sanshell/file4
-rw-r--r--. 1 root root 0 Oct 18 11:19 /tmp/test-gsc/sanshell/file5

加上xargs 时就能传递过去了。

如果是cp这种命令。用大写I参数和{}作为存储。

[root@localhost xargs]# cat xargses.txt | xargs -I {} cp -v {} /tmp/test-gsc/hahahaha//tmp/test-gsc/sanshell/file1’ -> ‘/tmp/test-gsc/hahahaha/file1’
‘/tmp/test-gsc/sanshell/file2’ -> ‘/tmp/test-gsc/hahahaha/file2’
‘/tmp/test-gsc/sanshell/file3’ -> ‘/tmp/test-gsc/hahahaha/file3’
‘/tmp/test-gsc/sanshell/file4’ -> ‘/tmp/test-gsc/hahahaha/file4’
‘/tmp/test-gsc/sanshell/file5’ -> ‘/tmp/test-gsc/hahahaha/file5’

xargs 的选项:

xargs选项:
-0      当sdtin含有特殊字元时候,将其当成一般字符,像/'空格等
         例如:root@localhost:~/test#echo "//"|xargs  echo 
                   root@localhost:~/test#echo "//"|xargs -0 echo 
                  /
-a file   从文件中读入作为sdtin,(看例一)
-e flag  注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,
           当xargs分析到含有flag这个标志的时候就停止。(例二)
-p 当每次执行一个argument的时候询问一次用户。(例三)
-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。(例四)
-t 表示先打印命令,然后再执行。(例五)
-i 或者是-I,这得看Linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。(例六)
-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。(例七)
-s num 命令行的最大字符数,指的是xargs后面那个命令行的最大字符数。(例八)
-L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。
-l  同-L
-d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符(例九)
-x exit的意思,主要是配合-s使用。
-P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值