命令重定向, 就是将目前获得的数据转移到指定的地方.分为如下几种:
>
>>
1>
2>
1>>
2>>
<
1. > 与 >>
先看一个简单的例子. 若是执行ll指令, 会在屏幕上显示执行结果:
bash
[root@localhost yuechaotian]# pwd /home/yuechaotian [root@localhost yuechaotian]# ll 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl账号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 10:58 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc [root@localhost yuechaotian]# ll test/ 总用量 0 |
若是不想在屏幕上显示, 而是想把输出结果直接存储在指定的文件中, 可使用 > 或 >>
并发
# > 将输出结果以"覆盖"的形式存储在指定的文件中, 若文件不存在则自动建立. [root@localhost yuechaotian]# ll > test/ll.log [root@localhost yuechaotian]# cat test/ll.log 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl账号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:01 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc # >> 将输出结果以“追加”的形式存储在指定的文件中, 若文件不存在则自动建立。 [root@localhost yuechaotian]# ll test/ >> test/ll.log [root@localhost yuechaotian]# cat test/ll.log 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl账号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:01 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc 总用量 4 -rw-r--r-- 1 root root 569 12月 14 11:01 ll.log |
那么若是指令执行失败呢? 输出结果就不会保存到指定文件中:
app
[root@localhost yuechaotian]# ll testx > test/ll.log.2 ls: testx: 没有那个文件或目录 [root@localhost yuechaotian]# ll test/ 总用量 4 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 [root@localhost yuechaotian]# cat test/ll.log.2 [root@localhost yuechaotian]# |
2. 1> 2>
若是须要将输出的正确结果保存到一个文件中, 输出的错误结果保存到另外一个文件中,能够借助 1> 和 2>
dom
[root@localhost yuechaotian]# ll testx/ 1> test/ll.right 2> test/ll.wrong [root@localhost yuechaotian]# ll test/ 总用量 8 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 -rw-r--r-- 1 root root 0 12月 14 11:11 ll.right -rw-r--r-- 1 root root 40 12月 14 11:11 ll.wrong [root@localhost yuechaotian]# cat test/ll.right [root@localhost yuechaotian]# cat test/ll.wrong ls: testx/: 没有那个文件或目录 |
Linux是经过什么来判断的呢?由于每一个当前指令的执行结果都保存在环境变量“?”中,当指令执行成功时 ?=0,当指令执行失败时 ?=1。Linux就是经过它来判断输出结果保存到哪一个文件中的:
测试
[root@localhost yuechaotian]# ll test/ 总用量 8 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 -rw-r--r-- 1 root root 0 12月 14 11:11 ll.right -rw-r--r-- 1 root root 40 12月 14 11:11 ll.wrong [root@localhost yuechaotian]# echo $? 0 [root@localhost yuechaotian]# ll testx/ ls: testx/: 没有那个文件或目录 [root@localhost yuechaotian]# echo $? 1 |
若是想不论指令执行正确与否,都将结果输出到同一个指定文件中,须要借助 1> 和 2>&1:
this
[root@localhost yuechaotian]# ll testx/ 1> test/ll.all 2>&1 [root@localhost yuechaotian]# cat test/ll.all ls: testx/: 没有那个文件或目录 |
若是能够提早预知错误的结果,只想保存正确的输出结果,而删除掉错误的结果,有什么好办法么?能够将错误的输出直接保存到“垃圾筒”中,这就借助到一个设备 /dev/null
spa
[root@localhost yuechaotian]#ll testx/ 1> test/ll.right.only 2>/dev/null |
3. 1>> 2>>
一样地,若是想将正确的输出结果追加到一个文件中,错误的输出结果追加到另外一个文件中,就须要借助 1>> 和 2>> 了。它们的使用方法跟 1> 2> 相似,所不一样的就是执行结果是追加到指定文件中,而不是覆盖。
orm
[root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 [root@localhost test]# cat ll.right [root@localhost test]# cd subtest 1>> ll.right 2>> ll.wrong [root@localhost test]# cat ll.right [root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 -bash: cd: subtest: 没有那个文件或目录 [root@localhost test]# ll ../ 1>> ll.right 2>> ll.wrong [root@localhost test]# cat ll.right 总用量 60 -r-------- 1 root root 22 9月 4 16:31 adsl账号.txt -rw-r--r-- 1 root root 62 12月 14 11:02 ll.log -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w -rw-r--r-- 1 root root 6 12月 14 11:06 s.log drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:20 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc [root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 -bash: cd: subtest: 没有那个文件或目录 |
4. 1> 2>> 与 1>> 2>
若是想将正确的输出和错误的输出都“追加”到同一个文件中,怎么办?你想试试 1>> 和 2>>&1 吗:
token
[root@localhost test]# rm * [root@localhost test]# echo "right and wrong" 1>> ll.r 2>>&1 -bash: syntax error near unexpected token `&' [root@localhost test]# ll 总用量 0 |
是的,没有 2>>&1 这个语法,但有 2>&1 这个语法,把 1>> 和 2>&1 接合起来看呢:
crontab
[root@localhost test]# echo "right and wrong" 1>> ll.r 2>&1 [root@localhost test]# ll 总用量 4 -rw-r--r-- 1 root root 16 12月 14 11:39 ll.r [root@localhost test]# cat ll.r right and wrong [root@localhost test]# echo "right and wrong2" 1>> ll.r 2>&1 [root@localhost test]# cat ll.r right and wrong right and wrong2 [root@localhost test]# ll "right and wrong2" 1>> ll.r 2>&1 [root@localhost test]# cat ll.r right and wrong right and wrong2 ls: right and wrong2: 没有那个文件或目录 |
咱们看,使用 1>> 和 2>&1 达到了目的。那就是说 1> 和 2>,1>> 和 2>> 并非配对的关系,他们之间应该是能够交叉使用的。下面测试一下:
[root@localhost test]# pwd /home/yuechaotian/test [root@localhost test]# rm * # 1. 输出正确则“追加”,输出错误则“覆盖” [root@localhost test]# echo "right_append & wrong_cover" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover [root@localhost test]# cat ll.w [root@localhost test]# echo "right_append & wrong_cover2" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w [root@localhost test]# cd "right_append & wrong_cover2" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover2: 没有那个文件或目录 [root@localhost test]# cd "right_append & wrong_cover" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 #2. 输出正确则“覆盖”,输出错误则“追加” [root@localhost test]# echo "right_cover & wrong_append" 1> ll.r 2>> ll.w [root@localhost test]# cat ll.r right_cover & wrong_append [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 [root@localhost test]# cd "right_cover & wrong_append" 1> ll.r 2>> ll.w [root@localhost test]# cat ll.r [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 -bash: cd: right_cover & wrong_append: 没有那个文件或目录 |
一个小问题:若是输出正确则“追加”,输出错误则直接删除。怎么实现?
如今有两种方法了:1>> 2>> /dev/null 和 1>> 2>/dev/null。其实“追加”到垃圾筒与“覆盖”到垃圾筒效果是同样的。设备/dev/null就象一个无底洞,扔进去的东西瞬间就消失了。一样地,也能够将错误的输出保存起来,而将正确的输出“扔”到垃圾筒中。
5. <
< 的做用,就是将本来应该由键盘输入的数据经由文件读入。好比发送邮件,可使用键盘输入邮件内容,也可使用 < 将保存在磁盘中的文件读出,并发送出去
# 1. 使用键盘输入方式来发送邮件给yuechaotian [root@localhost test]# mail -s "hi, yuechaotian" yuechaotian Hi, I'm root. . Cc: [root@localhost test]# su - yuechaotian [yuechaotian@localhost ~]$ [yuechaotian@localhost ~]$ procmail -v procmail v3.22 2001/09/10 Copyright (c) 1990-2001, Stephen R. van den Berg Copyright (c) 1997-2001, Philip A. Guenther Submit questions/answers to the procmail-related mailinglist by sending to: And of course, subscription and information requests for this list to: Locking strategies: dotlocking, fcntl() Default rcfile: $HOME/.procmailrc It may be writable by your primary group Your system mailbox: /var/mail/yuechaotian [yuechaotian@localhost ~]$ cat /var/mail/yuechaotian From root@localhost.localdomain Sun Dec 14 12:10:08 2008 Return-Path: Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4A8U0001330 for ; Sun, 14 Dec 2008 12:10:08 +0800Received: (from root@localhost) by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4A86X001329 for yuechaotian; Sun, 14 Dec 2008 12:10:08 +0800 Date: Sun, 14 Dec 2008 12:10:08 +0800 From: root Message-Id: <200812140410.mBE4A86X001329@localhost.localdomain> To: yuechaotian@localhost.localdomain Subject: hi, yuechaotian Hi, I'm root. # 2. 那好,如今我将邮件内容保存到 mail.txt 中,使用 < 将 mail.txt 中的内容从新发送给yuechaotian [yuechaotian@localhost ~]$ su - root Password: [root@localhost ~]# cd /home/yuechaotian/ [root@localhost yuechaotian]# touch mail.txt [root@localhost yuechaotian]# echo > mail.txt "Hi, I'm root, I send this mail by using <." [root@localhost yuechaotian]# cat mail.txt Hi, I'm root, I send this mail by using <. [root@localhost yuechaotian]# mail -s "hi, yuechaotian" yuechaotian < mail.txt [root@localhost yuechaotian]# su - yuechaotian [yuechaotian@localhost ~]$ cat /var/mail/yuechaotian From root@localhost.localdomain Sun Dec 14 12:10:08 2008 Return-Path: Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4A8U0001330 for ; Sun, 14 Dec 2008 12:10:08 +0800Received: (from root@localhost) by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4A86X001329 for yuechaotian; Sun, 14 Dec 2008 12:10:08 +0800 Date: Sun, 14 Dec 2008 12:10:08 +0800 From: root Message-Id: <200812140410.mBE4A86X001329@localhost.localdomain> To: yuechaotian@localhost.localdomain Subject: hi, yuechaotian Hi, I'm root. From root@localhost.localdomain Sun Dec 14 12:33:12 2008 Return-Path: Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4XCun024530 for ; Sun, 14 Dec 2008 12:33:12 +0800Received: (from root@localhost) by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4XChc024529 for yuechaotian; Sun, 14 Dec 2008 12:33:12 +0800 Date: Sun, 14 Dec 2008 12:33:12 +0800 From: root Message-Id: <200812140433.mBE4XChc024529@localhost.localdomain> To: yuechaotian@localhost.localdomain Subject: hi, yuechaotian Hi, I'm root, I send this mail by using <. [yuechaotian@localhost ~]$ |
6. 什么时候使用命令重定向 *当屏幕输出的信息很重要,咱们须要将它保存起来时; *背景执行中的程序,不但愿它干扰屏幕正常的输出结果时; *一些系统的例行性命令(好比写在/etc/crontab中的文件)的执行结果,但愿它能够保存下来时; *一些执行命令,咱们已经知道可能的错误信息,因此想以 2> /dev/null 将它丢掉时; *错误信息与正确欣喜须要分别输出时; *其它须要使用命令重定向的状况时。