[原创] BSD的sh和csh的重定向操作符全解
[原创] BSD的sh和csh的重定向操作符全解
预先的知识一:
noclobber 禁止覆盖变量,设定 $noclobber 预设变量改变输出重定向特性
变量设定语法 set noclobber
取消变量设定语法 unset noclobber复制代码
这个 noclobber 变量,它的功能便是停止重定向符号“>”的覆盖(overwiting)已存在文件以及符号“>>”要将字符写入一个不存在的文件时,自动产生该文件的特性。
仅用两个例子让读者明白,设定后的实际使用状况。
例子一:
% ps axu > testfile
% set noclobber
% echo "test set noclobber" > testfile
testfile: File exists.
% echo "test set noclobber" >! testfile
%复制代码
例子二:
% set noclobber
% cat /etc/passwd >> nopass
nopass: No such file or directory
% cat /etc/passwd >>! nopass
%复制代码
预先的知识二:
输入输出位置文件句柄:
STDIN(标准输入/Standard Input) 0
STDOUT(标准输出/Standard output) 1
STDERR(标准错误/Standard error) 2
>file csh, sh
将 STDOUT(标准输出/Standard output) 重定向到文件
(command > file)
>>file csh, sh
将 STDOUT(标准输出/Standard output) 字符串加到文件内容之后
(command >> file)
<file csh, sh
将文件重定向到 STDIN(标准输入/Standard Input) 作为命令的输入
(command < file)
<<word csh, sh
读取在线输入直到word(结束输入时,结束行输入word), 并做输入内容的变量替换
(command <<word)
<<\word csh, sh
读取在线输入直到word(结束输入时,结束行输入word), 不做输入内容的变量替换
(command <<\word)
<<-word sh
读取在线输入直到word(结束输入时,结束行输入word), 忽略TABS(制表符)
(command <<-word)
>>!file csh
将 STDOUT(标准输出/Standard output) 字符串追加到文件内容之后,当设定 $noclobber 时,可重写文件。
(command >>! file)
>!file csh
将 STDOUT(标准输出/Standard output) 重定向到新文件,当设定 $noclobber 时,可重写文件。
(command >! file)
>&file csh
将 STDOUT(标准输出/Standard output) 及 STDERR(标准错误/Standard error) 重定向到文件
(command >& file)
>>& csh
将 STDOUT(标准输出/Standard output) 及 STDERR(标准错误/Standard error) 字符串追加到文件内容之后
(command >>& file)
<&digit sh
切换 STDIN(标准输入/Standard Input) 到文件句柄
(command >cmd.log 2<&1)
<&- sh
关闭 STDIN(标准输入/Standard Input)
(command <&-)
>&digit sh
切换 STDOUT(标准输出/Standard output) 到文件句柄
(command >cmd.log 2>&1)
>&- sh
关闭 STDOUT(标准输出/Standard output)
(command >&-)
>&! csh
将 STDOUT(标准输出/Standard output) 及 STDERR(标准错误/Standard error) 重定向到文件,当设定 $noclobber 时,可重写文件。
(command >&! file)
>>&! csh
将 STDOUT(标准输出/Standard output) 及 STDERR(标准错误/Standard error) 字符串加到文件内容之后,当设定 $noclobber 时,可重写文件。
(command >>&! file)
参考:
CSH Tutorial Jennifer Dudley
UNIX SHELL Quote Tutorial Bruce Barnett and General Electric Company
Shell Scripts csh (C-Shell) Burkhard Kirste
Unix Shell Scripts Norman Matloff
UNIX Tutorial Ming JIN MING
UNIX C Shell 程式設計 陳慶銳(kimi)
UNIX C Shell 網路農夫
个人见解,有不完全以及不正之处,敬请指出,谢谢。
[ 本帖最后由 HonestQiao 于 2005-11-16 20:28 编辑 ]