09 shell脚本 --002>和 |

1.重定向

  数据输入:键盘   ---标准输入,但是并不是唯一方式

        --stdin

        echo "123456" |passwd --stdin "username"

        例如:./useradd.sh < user.txt

  数据输出:显示器  ---标准输出,但是并不是唯一方式

       ls /etc/ > a.txt

  fd 文件标识符 0-9

        0  1  2

        0 --标准输出

        1 --标准输入

         2 --错误标准输入输出

  常见重定向符号:

        1.标准输出

        >  覆盖重定向,非常危险!!!

          set -C   关闭覆盖重定向功能

          >|    强制重定向

        >>   追加重定向,不覆盖

        2.标准输入

        <       a <  b

        trt   替换文件内容

          例:tr abc ABC < /etc/passwd   > /etc/passwd.bak  #把/etc/passwd里的所有abc替换成ABC

        <<  多行数据同时输入

          cat >> a.txt << EOF

          >1

          >2

          >3

          >EOF

        3.错误输出

          2 >

            不需要输出内容,只需要输出状态

            ls /etc/ > /dev/null

            if  [ $? -eq 0 ];then

              

          2 >>

          &>   &>>   ==   2& > 1

            ls /etc/ &> /dev/null  #不管前面正确与否,都放到/dev/null/里

2.管道   -tee

          tee     一路输入,两路输出

            tee /tmp/tee.out  //如果没有文件,会创建,默认如果有文件会覆盖

          find [范围] -name ab* -type f -perm 600 mtime +7 |xregs rm -rf

原文地址:https://www.cnblogs.com/wx00/p/11314260.html