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)
欢迎关注本站公众号,获取更多信息