BSD的sh和csh的重定向操作符全解

预先的知识一:

        noclobber 禁止覆盖变量,设定 $noclobber 预设变量改变输出重定向特性    

  1. 变量设定语法 set noclobber

  2.     取消变量设定语法 unset noclobber

复制代码

    这个 noclobber 变量,它的功能便是停止重定向符号“>”的覆盖(overwiting)已存在文件以及符号“>>”要将字符写入一个不存在的文件时,自动产生该文件的特性。
        
        仅用两个例子让读者明白,设定后的实际使用状况。
        例子一:       

  1. % ps axu > testfile

  2.         % set noclobber

  3.         % echo "test set noclobber" > testfile

  4.         testfile: File exists.

  5.         % echo "test set noclobber" >! testfile

  6.         %

复制代码

        例子二:       

  1. % set noclobber

  2.         % cat /etc/passwd >> nopass

  3.         nopass: No such file or directory

  4.         % cat /etc/passwd >>! nopass

  5.         %

复制代码

预先的知识二:

        输入输出位置文件句柄:
        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)

评论

暂无评论