04 pipes, redirects and environment variables

If Linux commands cannot be used in combination, work efficiency cannot be improved.

The five modes of redirection technology: standard overwrite output redirection, standard additional output redirection, error overwrite output redirection, error additional output redirection, and input redirection.

image.png
Input and output redirection

        Format: "Command A redirect symbol file"

        Input redirection refers to importing a file into a command, while output redirection refers to writing the data information originally to be output to the screen into a specified file. Compared with input redirection, output redirection is used more frequently, so output redirection is divided into two different technologies, standard output redirection and error output redirection, and two modes of clear write and append write. .        

        Standard input redirection (STDIN, file descriptor is 0): Input from the keyboard by default, or input from other files or commands.

        Standard output redirection (STDOUT, file descriptor is 1): The default output is to the screen.

        Error output redirection (STDERR, file descriptor 2): The default output is to the screen.

image.pngimage.png

            Remarks: Use ">" when inputting normal information into the file, and use "2>" when inputting error information into the file. If you don't distinguish between normal and error information, you can use "&>".

                        Example:

                                man bash> readme.txt #Standard output mode in redirection (write the information that the man bash command originally intended to be output to the screen into the file readme.txt)

                                echo "Welcome to Linux"> readme.txt #Overwrite writing in redirection technology

                                echo "Quality linux learning materials" >> readme.txt #Additional writing in redirection technology

                                ls -l linuxxxxxx 2> /root/stderr.txt #Record the error information in the /root/stderr.txt file 

                                wc -l <readme.txt #Use the content of the file as standard input information, import it into the command, and count the number of lines in the file.

                

         tee: The instruction will read data from the standard input device, output its content to the standard output device, and save it as a file, the syntax format: tee [parameter] [file].

        

Pipeline command symbol (| any gate)

        Format: "Command A | Command B"

        Function: Regard the standard normal data originally output to the screen by the previous command as the standard input of the next command.        

image.png

                    Example:

                grep "/sbin/nologin" /etc/passwd | wc -l #Find out all users who are restricted to log in to the system by matching keywords /sbin/nologin, and count the number of text lines

                ls -l /etc/ | more #View the file list and attribute information in the /etc directory by turning pages

                echo "user" | passwd --stdin root #When modifying a user's password, it is usually necessary to enter the password twice for confirmation, which will become a very fatal flaw when writing automated scripts. By combining the pipe character with the --stdin parameter of the passwd command, one command can be used to complete the password reset operation

                echo "Content" | mail -s "Subject" user # Send mail to other users of this machine

                mail -s "Readme" [email protected] << over #The delimiter between the mail command and input redirection is used. The purpose is to keep the user inputting content until the user enters his custom delimiter. End input.

                                grep /bin/bash /etc/passwd | wc -l #Query the number of users who can log in to the system

                                echo 123456 | passwd --stdin user #One command to set the user password

Wildcard on the command line

                *Represents matching zero or more characters

                ?Represents matching a single character

                [0-9] Represents a character that matches a single digit between 0-9

                [a,b,c] means to match any one of the three characters a, b, c, and no error will be reported if there is no match

                {a,b,c} matches the specified letter, there will be an error message if there is no match

                [az] represents the 26 lowercase letters of az

                [AZ] represents the 26 capital letters of AZ

image.png

                Example:

                    ls -l / dev / sda *

                    ls -l / dev / sda?

                    ls -l / dev / sda [0-9]

                    ls -l / dev / sda [135]


Commonly used escape characters

                Backslash (\): Make a variable after the backslash a simple string.

                Single quotation mark (''): escape all variables in it as simple strings.

                Double quotation marks (""): Keep the variable attributes in it, without escaping.

                Backquote (``): Return the result after executing the command.

                    Example:

                        echo `uname -a` Do not execute the command, output the command in characters


Environment variable

                Step 1: Determine whether the user enters the command (such as /bin/ls) in an absolute path or a relative path, and if so, execute it directly.

                Step 2: The Linux system checks whether the command entered by the user is an "alias command", that is, replace the original command name with a custom command name. You can use the alias command to create your own command alias in the format "alias alias=command". To cancel a command alias, use the unalias command, the format is "unalias alias".

                Step 3: The Bash interpreter determines whether the user entered an internal command or an external command. Internal commands are commands inside the interpreter and will be executed directly; and the user inputs external commands most of the time, and these commands are handed over to step 4 to continue processing. You can use "type command name" to determine whether the command entered by the user is an internal command or an external command.

                Step 4: The system searches for the command file entered by the user in multiple paths, and the variable that defines these paths is called PATH, which can be simply understood as a "little assistant to the interpreter", which tells the Bash interpreter to wait The location where the executed command may be stored, and then the Bash interpreter will obediently look in these locations one by one. PATH is a variable composed of multiple path values. Each path value is separated by a colon. The addition and deletion of these paths will affect the search of Linux commands by the Bash interpreter

image.png


                        After taking over a Linux system, it must first check whether there is a suspicious directory in the PATH variable before executing the command.

                        Use the export command to promote ordinary variables to global variables.

                        To make the declared variable take effect permanently, you need to change the /etc/profile or ~/.bashrc file.

                        Show all environment variables in the system: env (environment)

                        Show all variables: set                        


other

        Modify prompt   

                    PS1=Name (modify back, exit to exit, restart)

                    Permanently modify vim ~/.bashrc, add PS1=name in a new line in the file, save and exit, exit and then restart the terminal

 


Guess you like

Origin blog.51cto.com/14032821/2593403