当前位置:网站首页>Wildcards and command chemical reactions

Wildcards and command chemical reactions

2021-01-23 16:13:48 Raccoon bubble water

@

Linux wildcard

Symbol effect
“*” Match any number of , Single character . ( Commonly used )
Match a character . ( Almost no use )
[abcd] Match a single character in it
[a-z] Match a single character in it
[!abcd] Match a single character in it ( Take the opposite )
Linux Special symbols : Path and location dependent
~ User home directory
. Represents the current directory
“-” On behalf of the last time ( Equivalent to the current path ) The path of the user
.. On behalf of the parent directory
Linux Special symbols : Reference string command related
Single quotation marks (’’) What you see is what you get , By ’’ There will be no change in the content of
Double quotes (“”) Can parse variables or commands , At output , Same as without quotation marks “” To represent a whole
The quotation marks (``) Reference command can parse command , Equivalent to $()
Space assignment , If the content of the variable is blank, the assignment is incomplete . And when you output content , A string containing spaces is treated as a whole output , If there is an order in the content ( It's going to lead you down )、 Variable etc. , It's going to put variables first 、 The command parses the result , And then output the final content , If there are special characters such as spaces in the string , You may not be able to complete the output , So we need to add double quotation marks , Generally continuous string 、 Numbers 、 Path can be assigned and output without any quotation marks , But in the case of no quotation marks, it's better to use double quotation marks instead , Especially in variable assignment
**Linux Special symbols : other **
; Indicates the end of a command , The separation command
“#” notes
$ 1. String preceded by $, Represents the string variable content 2. Normal user prompt
\ Escape sign , The translator
{} 1. Generating sequences 2. The variable represents the segmentation effect
1) stay Bash China means not , The meaning of taking the opposite Same as ^ 2) stay vi/vim It's compulsory 3)“!ls” Find out the most recent ls Start with the command and run
| Pass the command in front of the pipeline to the command in the back for further processing
& Put the program into the background running character
&& Previous command executed successfully , After executing the next command
Linux Special symbols : Redirection series special characters
0 Represents standard input (stdin), coordination < or << Use , Data flow from right to left
1 Indicates standard output (stdou), coordination > or >> Use , Data flow from left to right
2 The standard error (stderr), coordination > or >> Use , Data flow from left to right
0< or < Standard input redirection , Empty existing content , Data flows from files to commands for processing
0<< or << Append input redirection , Add content to the bottom , Data flows from files to processing commands
1> or > Standard output redirection , Normal output redirection to file , It will empty the existing content
1>> or >> Standard output append redirection , Append the content to the bottom of the file , Don't empty existing content
2> Error output redirection , Redirect standard error content to file , If there is content in the file, clear it
2>> Error output append redirection , Append the standard error content to the bottom of the file , It won't empty the existing content
“>/dev/null" Black holes
2>&1 &> The wrong symbol is appended with the correct one

; A semicolon

effect The separator for the command Separate multiple commands

No matter whether the previous command is executed successfully or not, it will continue to be executed later

Case study How to be in shell In a single line mkdir oldboy touch oldboy/test.txt rm oldboy/test.txt

[root@backup data]# mkdir -p /root/oldboy;touch /root/oldboy/test.txt;rm /root/oldboy/test.txt
rm: Delete normal empty file  "/root/oldboy/test.txt"?
[root@backup ~]# ll
 Total usage  4
drwxr-xr-x 2 root root 4096 1 month   21 21:10 data
drwxr-xr-x 2 root root   22 1 month   21 21:17 oldboy

&&

effect The previous command must be executed successfully In the execution of the following command Otherwise, the following commands will not be executed

error

[root@backup ~]# ll 123 && mkdir old
ls:  cannot access 123:  There is no file or directory 

correct

[root@backup ~]# ls data/ && mkdir old            vmware-vmsvc.log
ks-script-AShK6w.log  vmware-network.1.log  X.log
ks-script-nf4aZ1.log  vmware-network.2.log  yum.log
[root@backup ~]# ll
 Total usage  4
drwxr-xr-x 2 root root 4096 1 month   21 21:10 data
drwxr-xr-x 2 root root    6 1 month   21 21:26 old
drwxr-xr-x 2 root root   22 1 month   21 21:17 oldboy

: and & Use a combination of

[root@backup ~]# touch oldboy.txt ; ll -d data/ && touch du.txt
drwxr-xr-x 2 root root 4096 1 month   21 21:10 data/
[root@backup ~]# ll
 Total usage  4
drwxr-xr-x 2 root root 4096 1 month   21 21:10 data
-rw-r--r-- 1 root root    0 1 month   21 21:28 du.txt
drwxr-xr-x 2 root root    6 1 month   21 21:26 old
drwxr-xr-x 2 root root   22 1 month   21 21:17 oldboy
-rw-r--r-- 1 root root    0 1 month   21 21:28 oldboy.txt

||

effect The previous command must fail In the execution of the following command

[root@backup ~]# ll qwe || mkdir du
ls:  cannot access qwe:  There is no file or directory 
[root@backup ~]# ll
 Total usage  4
drwxr-xr-x 2 root root 4096 1 month   21 21:10 data
drwxr-xr-x 2 root root    6 1 month   21 21:31 du

& Linux In the system Put the current process into the background

The difference between quotation marks :

Without quotes Variable can be resolved Same as quotation marks
Double quotes Variable can be resolved Take the discontinuous content as a string
Single quotation marks You can't parse variables What you see is what you get

quotes

Case study

Without quotes

[root@backup ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Double quotes

Case study

[root@backup ~]# echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Single quotation marks

== Case study ==

[root@backup ~]# echo '$PATH'
$PATH

"*" Express all

== Case study ==
Find all arbitrary characters beginning with .txt Final document

[root@backup ~]# ls *.txt
du.txt  oldboy.txt

 Find all old Opening file 

[root@backup ~]# ls old*
oldboy.txt
old:
oldboy:
test.txt

 Find all to old At the beginning and .txt At the end of the 

[root@backup ~]# ll old*.txt
-rw-r--r-- 1 root root 0 1 month   21 21:28 oldboy.txt

[root@backup ~]# find ./ -type f -name 'old*.txt'
./oldboy.txt

? For any character

  [root@backup ~]# ll d?.txt
    -rw-r--r-- 1 root root 0 1 month   21 21:28 du.txt

brackets []

effect Match a single character in the period

[root@oldboy-lnb ~/20210118]#ll [a-z][a-z][a-z][0-9][0-9].txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu00.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu01.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu02.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu03.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu04.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu05.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu06.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu07.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu08.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu09.txt
-rw-r--r-- 1 root root 0 Jan 21 11:55 stu10.txt

{} Curly braces

effect : You can generate series The variable represents the function of separation

[root@oldboy-lnb ~/20210118]#echo oldboy.txt{,.bak}
oldboy.txt oldboy.txt.bak
[root@oldboy-lnb ~/20210118]#cp oldboy.txt{,.bak}
[root@oldboy-lnb ~/20210118]#ll oldboy.txt{,.bak}
-rw-r--r-- 1 root root 0 Jan 21 12:06 oldboy.txt
-rw-r--r-- 1 root root 0 Jan 21 12:21 oldboy.txt.bak

! Negate the contents in brackets

[root@backup ~]# ll [!ol]*
-rw-r--r-- 1 root root    0 1 month   21 21:28 du.txt

data:
 Total usage  2536
-rw------- 1 root root   34270 1 month   21 20:33 anaconda.log
-rw------- 1 root root  427157 1 month   21 20:33 audit.log
-rw------- 1 root root   37274 1 month   21 20:33 boot.log
-rw------- 1 root root    5465 1 month   21 20:33 ifcfg.log
-rw------- 1 root root 1607656 1 month   21 20:33 journal.log
-rw------- 1 root root       0 1 month   21 20:33 ks-script-AShK6w.log
-rw------- 1 root root       0 1 month   21 20:33 ks-script-nf4aZ1.lo

	[root@oldboy-lnb ~/20210118]#ll [!!so]*
	-bash: !so]*: event not found

\ escape

[root@oldboy-lnb ~/20210118]#ll [!\!so]*
	-rw-r--r-- 1 root root  0 Jan 21 12:11 1
	-rw-r--r-- 1 root root  0 Jan 21 12:11 123
	-rw-r--r-- 1 root root 41 Jan 21 12:06 1.txt
	-rw-r--r-- 1 root root  0 Jan 21 12:06 awk-oldboy
	-rw-r--r-- 1 root root  0 Jan 21 12:06 lidao-oldboy.txt

mark wildcard Match find file name All commands support

 Regular is not supported in normal commands    But wildcards have a special meaning in regularization 

Redirection symbol

effect : Process the contents of the file

">" Standard correct output redirection , Clear first and then write
2> Standard error output redirection
">>" Standard error append output redirection
2>> Standard error append output redirection
0 Represents standard input (stdin), coordination < or << Use , Data flow from right to left
1 Indicates standard output (stdou), coordination > or >> Use , Data flow from left to right
2 The standard error (stderr), coordination > or >> Use , Data flow from left to right
0< or < Standard input redirection , Empty existing content , Data flows from files to commands for processing
0<< or << Append input redirection , Add content to the bottom , Data flows from files to processing commands
1> or > Standard output redirection , Normal output redirection to file , It will empty the existing content
1>> or >> Standard output append redirection , Append the content to the bottom of the file , Don't empty existing content
2> Error output redirection , Redirect standard error content to file , If there is content in the file, clear it
2>> Error output append redirection , Append the standard error content to the bottom of the file , It won't empty the existing content
" >/dev/null" Black holes
2>&1 &> That is, the standard error is redirected to the file just like the standard output

echo practice

echo Standard correct output redirection , Clear first and then write

   [root@cs /]# echo oldboy > oldboy.txt    Enter the content into the file 
    [root@cs /]# cat oldboy.txt 
    oldboy
    [root@cs /]# echo test > oldboy.txt  
    [root@cs /]# cat oldboy.txt 
    test			 covers oldboy

echo Error output redirection

[root@cs /]# ll hahha.txt > oldboy.txt
ls:  cannot access hahha.txt:  There is no file or directory 
[root@cs /]# ll 3.txt 2> oldboy.txt    Error output redirection 
[root@cs /]# cat oldboy.txt 
ls:  cannot access 3.txt:  There is no file or directory 

echo Add redirection correctly

[root@cs /]# echo oldboyjj >> oldboy.txt    Add content to  oldboy.txt 
[root@cs /]# cat oldboy.txt 
ls:  cannot access 3.txt:  There is no file or directory 
oldboyjj

echo Error append redirection

[root@cs /]# llll 1.txt 2>> oldboy.txt   Add the error report to oldboy.txt
[root@cs /]# cat oldboy.txt 
ls:  cannot access 3.txt:  There is no file or directory 
oldboyjj
-bash: llll:  Command not found 

echo Receive both correct and error messages

[root@cs /]# echo hahch > oldboy.txt 2>oldboy.txt 
[root@cs /]# 
[root@cs /]# cat oldboy.txt 
hahch
[root@cs /]# echo hahh >> oldboy.txt  2>> oldboy.txt lll
[root@cs /]# cat oldboy.txt 
hahch
hahh lll
root@cs /]# ech gagag >> oldboy.txt  2>>oldboy.txt 
[root@cs /]# cat oldboy.txt 
hahch
hahh lll
-bash: ech:  Command not found 

The second way :

[root@cs /]# ech heheh >> oldboy.txt 2>&1
[root@cs /]# cat oldboy.txt 
hahch
hahh lll
-bash: ech:  Command not found 
-bash: ech:  Command not found 

The third method

  [root@cs /]# echo hahah &>> oldboy.txt 
    [root@cs /]# cat oldboy.txt 
    hahch
    hahh lll
    -bash: ech:  Command not found 
    -bash: ech:  Command not found 
    hahah

Ps : The third one is recommended

版权声明
本文为[Raccoon bubble water]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123161236699l.html