Linux learning-pipeline and redirection

Shell

Pipelines and redirects

  • Pipes and pipe symbols

    • The pipeline is also one of the ways of process communication
    • Pipe character "|" to pass the result of the previous command execution to the following command
      • ps | cat
      • echo 123 | ps
      • If there are external commands on both sides of the pipe character, a child process will be established. If you use internal commands, it is recommended to encapsulate the commands in the script before executing. If not necessary, avoid using internal commands within the pipe character.
  • Subprocesses and subshells

    • The child shell is a process created by the parent shell, and the parent shell creates the child shell and calls the fork function.
    • Built-in commands do not create sub-shells and external commands create sub-shells.
    • Colon usage: The colon can represent eternal truth (equivalent to the TRUE keyword) such as while:; do ... done (while loop condition is always true); secondly, the colon can clear a file,:> log redirects the colon to the file The content of the log file is cleared, so:> naming is a commonly used command to clear files; then the most important usage of the colon is: do nothing, only do parameter expansion.
    • The parentheses structure can force the commands to run in the subshell
    • Detailed explanation of subprocesses and subshells
  • Redirect symbol

    • A process will open standard input, standard output, error output three file descriptors by default
    • Enter the redirection symbol "<"
      • read var < /path/to/a/file
    • Output redirection symbol
      • ">" Clear the file before entering
      • ">>" file remains unchanged, additional input at the end
      • "2>" Output the wrong information to the specified file
      • "&>" Whether correct or incorrect information is output to the specified file
      • 例:echo 123 > /path/a/file
    • Combination of input and output redirection
      • cat > /path/a/file << EOF
      • I am #USER
      • EOF

Guess you like

Origin www.cnblogs.com/chenri/p/12677398.html