02, the pipeline operators and redirection

1, redirection

    Program instructions = data +

      (Command) (variable)

    In the process, how the data input? How output?

    Data Input: keyboard - the standard input, but the input is not the only way;

         --stdin

         echo "123456" | passwd - stdin "username" does not enter the interface

         For example: function add user useradd.sh user.txt text document 1000 users

              ./useradd.sh a

              ./useradd.sh b

              ...

              ./useradd.sh < user.txt

         while line;do

            $ Line loop

         done < /etc/passwd

    Output data: Display - standard output, but the output is not the only way

         ls /etc/ >a.txt

    fd file identifier 0-9-- equivalent to the document classification;

        0  1  2

        0 - standard output

        1 - Standard Input

        2 - O Error (standard error)

    Common redirection symbols

        1, the standard output

          > Cover redirection, very dangerous

              set -C: cancel cover redirection + C

              > | Forced redirection

          >> append redirection

              Does not cover

        2, the standard input

          <Tr replace the contents of a file

              tr    set1 [set2]  <file.txt

          << The multiple rows of data simultaneously input

              cat >> a.txt <<EOF

               >1

               >2

               >3

               >EOF

        3, error output

          2>  2>>

          Extended: no output content, only the output state

              ls  /etc/  >  /dev/null

              if [ $? -eq 0 ];then

                Loop

              be

          ls > /dev/null 2> /dev/null

          &> &>>  == 2& > 1

              ls  /etc/  &>  /dev/null

2, the pipe - tee

    command1 | command2 | command3 | ... ...

    After the results of the previous command to a command;

    [Linux Thought: The Combination achieve great little feature function]

    free - m | grep "^Men" | cut -d' ' -f19

    free - m | grep "^Men" | awk '{print $3}'

 

    tee all the way to the two input-output

        tee /tmp/tee.out // If no file is created, the default file exists and if there is content, will be covered;

 

    Exercise: The first five rows content / etc / passwd file is converted to /tmp/passwd.out save a file to uppercase;

          head -5 /etc/passwd | tr [a-z] [A-Z] > /tmp/passwd.out

         Log on to the system after three current information into aggregated user information is saved to the capital after /tmp/who.out

          who | tail -3 | cut -d' ' -f1 | tr [[:lower:]] [[:upper:]]  tee /tmp/who.out

    

    tail end of the file to see how many rows (default ten lines)

        -n tail -n 5 / etc / passwd == abbreviated tail -5 / etc / passwd

        -f view real-time file updates

            tail -f /var/log/message

    head to see how many lines the file header (default ten lines)

        -n # # before the line, abbreviated - #

    10-20 take line: head -20 / etc / passwd | tail -10

Guess you like

Origin www.cnblogs.com/cnxy168/p/11317302.html