Redirection Operation
重定向和管道操作
All Shell
< file > file >> file
Cmd1 | cmd2
0 stdin, 1 stdout, 2 stderr
sh, ksh
0< file 1> file 2> file 1>> file 2>> file
<&m >&m m>&n, m,n is file descriptor
<&-, close standard input
>&-, close standard output
m<&- m>&-, close file descriptor m
examples:
grep “John” 0< tempfile
ls –l foo 2> error.log
cat lab1 lab2 lab3 1> cat.output 2> cat.err
cat lab1 lab2 lab3 1> cat.output.err 2>&1
pay attention to the redirection instruction order
cat lab1 lab2 lab3 2>&1 1> cat.output.err
ls –l foo 1>> output.dat 2>> error.log
ksh
set –o noclobber, set noclobber to prevent overwritting files accidently.
set +o noclobber
>| file, ignore noclobber and assign stdout to ‘file’
>>| file, ignore noclobber and append stdout to ‘file’
csh
>& file, output and error redirection
>! file, ignore noclobber and assign stdout to ‘file’
>>! file, ignore noclobber and append stdout to ‘file’,
if file does not exit, create it.
>>&! file, ignore noclobber, append stdout and stderr to ‘file’
cmd1 |& cmd2, connect stdout and stderr of cmd1 to stdin of cmd2
(cmd>/dev/tty)>&file, redirect stderr of cmd to ‘file’
set noclobber
unset noclobber
/dev/tty
/dev/null
抛弃输出数据, 如 $ echo "hello" > /dev/null
获取空文件,如 $ cat /dev/null > messages