Nik*_*yko 3 ksh io-redirection
<>– 几个月前我在一个站点上看到的这个操作员,但我不记得它是什么意思。也许我错了,而 ksh 没有<>。
a<&b– 我知道这个操作符将输入流a与输出流合并b。我对吗?但是我不知道在哪里可以使用它。你能给我举个例子吗?
>&-——我对此一无所知。例如,是什么2>&-意思?
从http://www.manpagez.com/man/1/ksh/:
<>word Open file word for reading and writing as standard out-
put.
<&digit The standard input is duplicated from file descriptor
digit (see dup(2)). Similarly for the standard output
using >&digit.
<&- The standard input is closed. Similarly for the standard
output using >&-.
Run Code Online (Sandbox Code Playgroud)
您将通过键入 找到所有这些详细信息man ksh。
特别是2>&-意味着:关闭标准错误流,即命令不再能够写入 STDERR,这将破坏要求它可写的标准。
要了解文件描述符的概念,(如果在 Linux 系统上)您可以查看/proc/*/fd (和/或/dev/fd/*):
$ ls -l /proc/self/fd
insgesamt 0
lrwx------ 1 michas users 1 18. Jan 16:52 0 -> /dev/pts/0
lrwx------ 1 michas users 1 18. Jan 16:52 1 -> /dev/pts/0
lrwx------ 1 michas users 1 18. Jan 16:52 2 -> /dev/pts/0
lr-x------ 1 michas users 1 18. Jan 16:52 3 -> /proc/2903/fd
Run Code Online (Sandbox Code Playgroud)
文件描述符 0(又名 STDIN)默认用于读取,fd 1(又名 STDOUT)默认用于写入,fd 2(又名 STDERR)默认用于错误消息。(在这种情况下,fd 3 由ls实际读取该目录。)
如果您重定向内容,它可能如下所示:
$ ls -l /proc/self/fd 2>/dev/null </dev/zero 99<>/dev/random |cat
insgesamt 0
lr-x------ 1 michas users 1 18. Jan 16:57 0 -> /dev/zero
l-wx------ 1 michas users 1 18. Jan 16:57 1 -> pipe:[28468]
l-wx------ 1 michas users 1 18. Jan 16:57 2 -> /dev/null
lr-x------ 1 michas users 1 18. Jan 16:57 3 -> /proc/3000/fd
lrwx------ 1 michas users 1 18. Jan 16:57 99 -> /dev/random
Run Code Online (Sandbox Code Playgroud)
现在默认描述符不再指向您的终端,而是指向相应的重定向。(如您所见,您还可以创建新的 fd。)
另一个例子<>:
echo -e 'line 1\nline 2\nline 3' > foo # create a new file with three lines
( # with that file redirected to fd 5
read <&5 # read the first line
echo "xxxxxx">&5 # override the second line
cat <&5 # output the remaining line
) 5<>foo # this is the actual redirection
Run Code Online (Sandbox Code Playgroud)
你可以做这样的事情,但你很少必须这样做。