This is what Linux should learn: redirection, pipe characters, wildcards, escape characters, environment variables

Time:2021-6-9

Chapter 3: redirection, pipeline, environment variable

3.1 I / O redirection

  1. Definition: input redirection is to import the file into the command, and output redirection is to output the information originally displayed on the screen to the file. Compared with input redirection, output redirection is used more. The following focuses on output redirection

  2. The symbols and functions used for output redirection are shown below

    Symbol effect
    Command > file Redirecting standard output to a file (while emptying the original contents of the file)
    Command 2 > file Redirecting error output to a file (while emptying the original contents of the file)
    Command > > file Redirecting standard output to a file (appending after the original content in the file)
    Command 2 > > file Redirecting error output to a file (appending after the original content in the file)
    Command & > > file Redirecting standards and errors to a file (appending after the original content in the file)
  3. Examples

    1. #Enter the command ls in the user's home directory, and write the content output to the screen to the file list
      ls > list.txt

      image-20210415105025001

    2. #Error redirection
      cd list.txt 2> cd list.txt

      image-20210415105249144

    3. #The standard output redirection appends the original content in the file
      ls >> list.txt

      image-20210415105654101

3.2 pipeline symbol

  1. Command format: “command 1 | Command 2”

  2. Function: take the data originally input to the screen by the previous command as the standard input of the later command

  3. Examples

    1. #Find out the number of users who are restricted to login
      grep "/sbin/nologin" /etc/passwd | wc -l

      image-20210415112345579

    2. #Turn the page to view the list in the / etc directory
      ls -l /etc/ | more

      image-20210415112530536

    3. #In one word, complete the password reset operation
      echo "newpasswd" | passwd --stdin root

      image-20210415192250146

  4. The pipeline character is not limited to the combination of two commands. It can have “command 1 | Command 2 | command 3”

3.3 command line wildcards

  1. (*) means to match zero or more characters, (?) It means to match a single character, and adding characters ([0-9], [A-Z], [ABC]) in brackets means to match one of them

  2. Demo with / dev directory in rhel8

    1. #Output all files beginning with TTY
      ls -l /dev/tty*

      image-20210415194050824

    2. #Output TTY file with end symbol in the range of 0-9
      ls -l /dev/tty[0-9]

      image-20210415194242287

    3. #Output TTY file with only one character
      ls -l /dev/tty?

      image-20210415194408460

3.4 common escape characters

  1. Four common escape characters

    1. Backslash (\): makes a variable after a slash a simple string
    2. Single quotation mark (‘): escape where all variables are simple strings
    3. Double quotation marks (“): keep the variable properties without escaping
    4. Back quotation mark (‘): returns the result after executing the command
  2. Example: first define a variable

    image-20210415195213320

    1. #If we want to output "price is $5"“
      #Use escape (\)
      echo "Price is $$PRICE"

      image-20210415195619994

    2. #If you need the output value of a command, you can use the ` command`
      #For example, view the Linux version information of this machine
      echo uname -a

      image-20210415195745064

3.5 important environmental variables

  1. Definition of environment variables: Linux variable names are generally capitalized (conventional), we can extract the corresponding variable value through the variable name

  2. Format: “echo $variable name”

  3. Common variable names

    Variable name effect
    HOME User’s home directory
    SHELL The name of the shell interpreter that the user is using
    MAIL The path to save the message
    PATH Define the path for the interpreter to search for the user to execute the command
  4. Examples

    image-20210415201141391