Just learn Linux pipe symbols, redirection, environment variables

Input redirection refers to importing files into commands, while output redirection refers to writing data information that is originally output to the screen into a specified file. In daily study and work, we use output redirection more frequently than input redirection, so output redirection is divided into two different technologies: standard output redirection and error output redirection, and coverage Write and append write two modes

➢ Standard input redirection 0: Input from the keyboard by default, or input from other files or commands
➢ Standard output redirection 1: Output to the screen by default
➢ Error output redirection 2: Output to the screen by default

touch linuxprobe
ls -l linuxprobe
ls -l xxxx

insert image description here
insert image description here
Write the information originally output to the screen by the man bash command into the file readme.txt through standard output redirection, and then display the contents of the readme.txt file. The specific commands are as follows:

man bash > readme.txt
cat readme.txt

First write multiple lines of data to the readme.txt file (the file already contains the man command information from the previous experiment) by overwriting the write mode. It should be noted that when writing data to a file through overwriting mode, the content written last time will be overwritten every time, so only the last writing result will be in the final file:

echo "welcome to LinuxProbe1.Com" > readme.txt
echo "welcome to LinuxProbe2.Com" > readme.txt
echo "welcome to LinuxProbe3.Com" > readme.txt
echo "welcome to LinuxProbe4.Com" > readme.txt
cat readme.txt

Then write data to the readme.txt file once through the append writing mode, and then after executing the cat command, you can see the content of the file as shown below:

echo "Quality linux learning materials" >> readme.txt
cat readme.txt

Although both are output redirection technologies, there are still differences between the standard output and error output of the command. For example, to check the information of a file in the current directory, here we take the linuxprobe file as an example. Since this file really exists, the information originally to be output to the screen can be written to the file by using the standard output, but the wrong output redirection still outputs the information to the screen.

ls -l linuxprobe > /root/stderr.txt
ls -l linuxprobe 2> /root/stderr.txt

What should I do if I want to write the error message of the command to a file? This operation is particularly useful and practical when the user is executing an automated shell script, because it can record the error information during the entire script execution process into a file, which is convenient for troubleshooting after installation. Next, perform an experimental demonstration with a file that does not exist:

ls -l xxxxx > /root/stderr.txt
ls -l xxxx 2> /root/stdeerr.txt
cat /root/stderr.txt

Another common situation is that we don't want to distinguish between standard output and error output, as long as the command has output information, it will all be appended to the file. This is where the &>> operator is used:

ls -l linuxprobe &>> readme.txt
ls -l xxxxx &>> readme.txt

Input redirection is relatively unpopular, and the probability of encountering it at work will be smaller. The role of input redirection is to import the file directly into the command. Next, use input redirection to import the readme.txt file to the wc -l command, and count the number of content lines in the file:

wc -l < readme.txt
wc -l /etc/passwd

There is no file name, this is because the previously used "wc -l /etc/passwd" is a very standard "command + parameter + object" execution format, and this time "wc -l < ​​readme.txt" The content in the readme.txt file is imported into the command through the operator, and it is not executed as a command object. Therefore, the wc command can only read the information flow data, without the information of the file name.

pipeline command

➢ The command to find out the restricted login users is grep /sbin/nologin /etc/passwd;
➢ The command to count the number of text lines is wc–l.
What to do now is to pass the output value of the grep search command to the wc statistics command, that is, to pass the user information list that was originally output to the screen to the wc command for further processing, so you only need to put the pipe symbol into the two commands between, as follows:

grep /sbin/nologin/etc/passwd | wc -l

For example, view the file list and attribute information in the /etc directory by flipping pages (these contents will be displayed on the screen by default, which is not clear at all):

ls -l /etc/ | more

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 automation scripts. By combining the pipe character with the –stdin parameter of the passwd command, you can complete the password reset operation with a single command:

echo "linuxprobe" | 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 automation scripts. By combining the pipe character with the –stdin parameter of the passwd command, you can complete the password reset operation with a single command:

ps aux | grep bash

If you need to output the processed result of the pipe character to the screen and write it to a file at the same time, you can use it in conjunction with the tee command.

ps aux | grep bash | tee result.txt

Wildcards on the command line

One way to view the relevant permission attributes of all hard disk files in batches is as follows:

ls -l /dev/sda
ls -l /dev/sdal
ls -l /dev/sad2
ls -l /dev/sda3

As the name implies, wildcards are common symbols for matching information. For example, an asterisk (*) means matching zero or more characters, a question mark (?) means matching a single character, adding numbers [0-9] in square brackets means matching 0~ 9, and the letter [abc] inside the square brackets means to match any one of the three characters a, b, and c. The wildcards and their meanings in the Linux system are shown in the table (similar to regular expressions in python)
insert image description here

to match all files in the /dev directory that start with sda:


ls -l /dev/sda*

If you only want to view information about files whose file name starts with sda but is followed by another character, you need to use question marks for wildcarding:

ls -l /dev/sda?

In addition to using [0-9] to match a single number between 0 and 9, you can also use [135] to match only one of the 3 specified numbers; if no number 1 or 2 or 3 is matched, then won't show up:

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

Search for all configuration files ending in .conf in the /etc/ directory:

ls -l /etc/*.conf

Wildcards can not only be used to search for files or replace wildcarded characters, but can also be combined with commands to create files to create multiple files in one go. However, when creating multiple files, braces are required and fields are separated by commas:

touch {
    
    AA, BB, CC}.conf
ls -l *.conf

Some specified information can also be output using wildcards:

echo file{
    
    1,2 3,4,5}

Common escape characters

The 4 most commonly used escape characters are listed below.
➢ Backslash (\ ): make a variable after the backslash into a simple character.
➢ Single quote (''): Escape all the variables in it to a simple string.
➢ Double quotes (" "): retain the variable attributes inside, without escaping.
➢ Backtick (` `): Execute the command and return the result.

First define a variable named PRICE and assign it a value of 5, then output the string and variable information enclosed in double quotes:

PRICE=5
echo "Price is $PRICE"

echo "Price is $$PRICE"

echo "Price is \$$PRICE"

And if you only need the output value of a certain command, you can enclose the command in backticks like `command` to achieve the desired effect. For example, combine backticks with the uname -a command, and then use the echo command to view the local Linux version and kernel information:

echo `uname -a`

The functions of backslashes and backticks are more distinctive, and students generally do not make mistakes, but it is easy to confuse when to use double quotes, because in most cases, it seems that adding or not adding double quotes has the same effect:

echo AA B CC
echo "AA BB CC"

The difference between the two is that the user cannot know how many parameters there are in the first execution mode. Yes, not sure! Because it is possible to directly output "AA BB CC" as a whole parameter to the screen, and it is also possible to output AA, BB and CC to the screen separately. Moreover, even if you figure out the mechanism of echo command processing parameters, this situation still exists when using other commands.

Here is a simple trick for everyone. Although it may not be rigorous enough, it is absolutely simple: if there is a space in the parameter, add double quotation marks; if there is no space in the parameter, then do not add double quotation marks.

important environment variables

In the Linux system, variable names are generally in uppercase, and commands are in lowercase, which is a convention.

ls
rm anaconda-ks.cfg

You can use the alias command to create your own command alias, and the syntax format is "alias alias=command". To cancel a command alias, use the unalias command, and the syntax format is "unalias alias".

Cancel the alias set by the current rm command, and then try to delete the file:

unalias rm
rm initial -setup-ks.cfg

You can use "type command name" to determine whether the command entered by the user is an internal command or an external command:

type echo
type uptime

PATH is a variable composed of multiple path values, and each path value is separated by a colon. The addition and deletion of these paths will affect the Bash interpreter's search for Linux commands.

echo $PATH
PATH = $PATH:/root/bin
echo $PATH

insert image description here
Therefore, the same variable can have different values ​​depending on the identity of the user. For example, use the following command to see what values ​​the HOME variable has under different user identities

echo $HOME
su - linuxprobe

In fact, variables are composed of fixed variable names and variable values ​​set by users or systems. We can create variables by ourselves to meet work needs. For example, setting a variable named WORKDIR makes it easier for users to enter a deeper directory:

mkdir /home/workdir
WORKDIR=/home/workdir
cd $WORKDIR
pwd

However, such variables are not global, have limited scope, and cannot be used by other users by default:

su linuxprobe

If your job requires it, you can use the export command to promote it to a global variable so that other users can use it:

export WORKDIR
su linuxprobe

If you do not use this variable in the future, you can execute the unset command to cancel it:

unset WORKDIR

What command can be used to convert a general variable named LINUX into a global variable?

Answer: export LINUX.

Guess you like

Origin blog.csdn.net/AdamCY888/article/details/131291314