linux 脚本2> 1,在shell中,“2>&1”是什么意思?

关于重定向的一些技巧

这方面的一些语法特殊性可能具有重要的行为。有一些关于重定向的小样本,STDERR, STDOUT,以及争论定序.

1-覆盖还是追加?

符号>平均重定向.>平均

作为一个完整的文件发送到,如果存在,则重写目标(请参见

noclobberBash特性

#3晚些时候)。

>>平均

除发送如果存在,将追加到目标。

在任何情况下,如果文件不存在,将创建它们。

2shell命令行是依秩序而定的!

为了测试这个,我们需要一个简单的命令,它将对两个输出发送一些内容。:$ ls -ld /tmp /tnt

ls: cannot access /tnt: No such file or directory

drwxrwxrwt 118 root root 196608 Jan  7 11:49 /tmp

$ ls -ld /tmp /tnt >/dev/null

ls: cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt 2>/dev/null

drwxrwxrwt 118 root root 196608 Jan  7 11:49 /tmp

(期望您没有一个名为/tnt当然;)好吧,我们有了!

那么,让我们看看:$ ls -ld /tmp /tnt >/dev/null

ls: cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt >/dev/null 2>&1$ ls -ld /tmp /tnt 2>&1 >/dev/null

ls: cannot access /tnt: No such file or directory

最后一个命令行转储STDERR在控制台上,这似乎不是预期的行为.但是.。

如果你想做一些后滤波关于一个输出,另一个或两者:$ ls -ld /tmp /tnt | sed 's/^.*$//'ls: cannot access /tnt: No such file or directory

7 12:02 /tmp --->$ ls -ld /tmp /tnt 2>&1 | sed 's/^.*$//'

$ ls -ld /tmp /tnt >/dev/null | sed 's/^.*$//'ls:

cannot access /tnt: No such file or directory

$ ls -ld /tmp /tnt >/dev/null 2>&1 | sed 's/^.*$//'$ ls -ld /tmp /tnt 2>&1 >/dev/null | sed 's/^.*$//'

请注意,这一段中的最后一个命令行与我所写的前一段完全相同。似乎不是预期的行为(因此,这甚至可能是一种预期的行为)。

嗯,有一些关于重定向的小窍门,因为对这两个输出执行不同的操作:$ ( ls -ld /tmp /tnt | sed 's/^/O: /' >&9 ) 9>&2  2>&1  | sed 's/^/E: /'O: drwxrwxrwt 118 root root 196608 Jan  7 12:13 /tmp

E: ls: cannot access /tnt: No such file or directory

诺塔:&9描述符会自发地出现,因为) 9>&2.

增编:诺塔!的新版本巴什 (>4.0)有一个新的特性和更性感的语法来做这样的事情:$ ls -ld /tmp /tnt 2> >(sed 's/^/E: /') > >(sed 's/^/O: /')O: drwxrwxrwt 17 root root 28672 Nov  5 23:00 /tmp

E: ls: cannot access /tnt: No such file or directory

最后,对于这样的级联输出格式:$ ((ls -ld /tmp /tnt |sed 's/^/O: /' >&9 ) 2>&1 |sed 's/^/E: /') 9>&1| cat -n     1  O: drwxrwxrwt 118 root root 196608 Jan  7 12:29 /tmp

2  E: ls: cannot access /tnt: No such file or directory

增编:诺塔!相同的新语法,在这两种方式中:$ cat -n  >(sed 's/^/E: /') > >(sed 's/^/O: /'))

1  O: drwxrwxrwt 17 root root 28672 Nov  5 23:00 /tmp     2  E: ls: cannot access /tnt: No such file or directory

哪里STDOUT通过一个特定的过滤器,STDERR最后,将两个输出合并到另一个输出,通过第三个命令筛选器。

三言两语noclobber选项和>|句法

那是关于覆写:

当set -o noclobber指示巴什不覆盖任何现有文件,则>|语法允许您通过这个限制:$ testfile=$(mktemp /tmp/testNoClobberDate-XXXXXX)$ date > $testfile ; cat $testfileMon Jan  7 13:18:15 CET 2013$ date > $testfile ;

cat $testfileMon Jan  7 13:18:19 CET 2013$ date > $testfile ; cat $testfileMon Jan  7 13:18:21 CET 2013

文件每次都会被覆盖,现在:$ set -o noclobber

$ date > $testfile ; cat $testfile

bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing fileMon Jan  7 13:18:21 CET 2013$ date > $testfile ; cat $testfile

bash: /tmp/testNoClobberDate-WW1xi9: cannot overwrite existing fileMon Jan  7 13:18:21 CET 2013

通过.>|:$ date >| $testfile ; cat $testfileMon Jan  7 13:18:58 CET 2013$ date >| $testfile ; cat $testfileMon Jan  7 13:19:01 CET 2013

取消此选项和/或查询是否已设置。$ set -o | grep noclobber

noclobber           on

$ set +o noclobber

$ set -o | grep noclobber

noclobber           off

$ date > $testfile ; cat $testfileMon Jan  7 13:24:27 CET 2013$ rm $testfile

最后一招还有更多.。

重定向双管齐下从给定的命令输出,我们看到正确的语法可以是:$ ls -ld /tmp /tnt >/dev/null 2>&1

为了这个特制情况下,有一个快捷语法:&>..或>&$ ls -ld /tmp /tnt &>/dev/null

$ ls -ld /tmp /tnt >&/dev/null

诺塔:如果2>&1存在,1>&2也是正确的语法:$ ls -ld /tmp /tnt 2>/dev/null 1>&2

4B-现在,我让你想想:$ ls -ld /tmp /tnt 2>&1 1>&2  | sed -e s/^/++/++/bin/ls: cannot access /tnt: No such file or directory++drwxrwxrwt 193 root root 196608 Feb

9 11:08 /tmp/$ ls -ld /tmp /tnt 1>&2 2>&1  | sed -e s/^/++//bin/ls: cannot access /tnt: No such file or directory

drwxrwxrwt 193 root root 196608 Feb  9 11:08 /tmp/

4C-如果你对更多信息

你可以通过点击阅读好的手册:man -Len -Pless\ +/^REDIRECTION bash

在.巴什控制台;-)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值