day12: use of wildcards

In the work, it is inevitable that you can't remember the file name and need to make fuzzy matching or match specific objects. At this time, wildcards will be used in the command.

For example, chestnuts:

afei@ubuntu:~/workspace$ ls -l file?
Use Wildcards  * Matches any character except log.text Everything else is shown 
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 file1
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 file2
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 fileE
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 fileq
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 fileR
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 filew
afei@ubuntu:~/workspace$ 
afei@ubuntu:~/workspace$ ls -l file[1-9]
Use Wildcards  [1-9] Match digit 1-9 The last digit of the file name is a number
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 file1
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 file2
afei@ubuntu:~/workspace$ 
afei@ubuntu:~/workspace$ ls -l file[a-z]
Use Wildcards  [a-z] Matching letters a-z The last lowercase letter of the file name is displayed
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 fileq
-rw-rw-r-- 1 afei afei 0 Oct 20 05:36 filew
afei@ubuntu:~/workspace$ 
afei@ubuntu:~/workspace$ cd ..
afei@ubuntu:~$ ls -l workspace/*.text
 Use Wildcards  * Match any character, log.text It shows up 
-rw-rw-r-- 1 afei afei 0 Oct 20 05:44 workspace/log.text
afei@ubuntu:~$ 

Wildcards in linux:

wildcard

meaning

*

Any character

?

Any single character

[a-z]

Single lowercase letter

[A-Z]

Single capital letter

[a-Z]

Single letter

[0-9]

Single number

[[:alpha:]]

Any letter

[[:upper:]]

Any capital letter

[[:lower:]]

Any lowercase letter

[[:digit:]]

All numbers

[[:alnum:]]

Any letter plus number

[[:punct:]]

punctuation

Metacharacter:

character

explain

Command separator. At the end of the previous command, ignore its return value and continue to execute the next command

&

The biggest advantage of running commands in the background is that you can continue to enter commands on the same command line without waiting for the end of command execution

=

Variable name = value, assign a value to the variable. Note that "=" is immediately followed by variable name and value, and there should be no space in the middle

$

Variable value, "$variable name" is replaced by "value" of shell variable. To avoid confusion in text connection, please use ${variable name}

>

Output redirection, prog > file redirects standard output to a file (overwriting the contents of the original file)

>>

Output redirection, prog > > file append standard output to file

<

Input redirection, prog < file get standard input from file

|

Pipeline command, for example, p1 | p2 takes the standard output of p1 as the standard input of p2

()

Execute commands in a sub shell, either for computation or for command substitution ($(command))

{}

Execute a command in the current shell, or use the delimited scope of variable replacement (for example, ${variable name} usage above)

&&

The next command cannot be executed until the previous command is executed successfully. Example: p1 & & p2: if p1 is executed successfully, p2 will be executed; otherwise, p2 will not be executed

||

The next command cannot be executed until the previous command fails. Example: p1 | p2. If p1 is executed successfully, p2 will not be executed. On the contrary, p2 will be executed

!

Execute the commands in the history (history list), such as! one

~

User home directory

For example, chestnuts:

~Home directory and; Command separator, > redirector

afei@ubuntu:~/workspace$ 
afei@ubuntu:~/workspace$ cd ~
Return to user home directory
afei@ubuntu:~$ 
afei@ubuntu:~$ touch test.text; echo "This is a test file" > test.text
 establish test.text File, and redirect the string into the file after creation
afei@ubuntu:~$ 
afei@ubuntu:~$ ls -l test.text 
-rw-rw-r-- 1 afei afei 20 Oct 20 06:38 test.text
afei@ubuntu:~$ 
afei@ubuntu:~$ cat test.text 
This is a test file
afei@ubuntu:~$ 

=Variable assignment character and $variable value character

afei@ubuntu:~$ NAME=afei
afei@ubuntu:~$ echo $NAME
afei
afei@ubuntu:~$ 

{}

afei@ubuntu:~$ echo $NAMEhahaha

Output is null, variable not found NAMEhahaha
afei@ubuntu:~$ 
afei@ubuntu:~$ echo ${NAME}hahaha
afeihahaha

Find variable NAME And put the character hahaha Splice to back
afei@ubuntu:~$ 

Others can explore by themselves!!!

Escape character:

When using linux or writing shell scripts, we often encounter some special characters, which will conflict with variables and file names, resulting in execution errors. At this time, in order to more clearly express the meaning of the command to the interpreter, we need to use escape characters to clearly express what we want the interpreter to do.

Common escape characters are:

Backslash (\): changes a variable after the backslash to a pure character.

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

Double quotation mark (""): keep the variable attribute in it without escape.

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

For example, chestnuts:

Use of single quote '' and double quote '':

afei@ubuntu:~$ NAME=afei
 Define variables NAME Value is afei
afei@ubuntu:~$ 
afei@ubuntu:~$ echo 'This is $NAME'
This is $NAME
 Using single quotation marks, all output contents are escaped into character output without referencing variable values
afei@ubuntu:~$ 
afei@ubuntu:~$ echo "This is $NAME"
This is afei
 Using double quotation marks, the variable in the output content is not escaped as a character, and the variable value is referenced
afei@ubuntu:~$ 

/Use of symbols:

Output the NAME variable value to the screen. When the \ symbol is used, $is escaped as a pure character and no longer has the function of metacharacter, so the NAME variable value is not obtained

afei@ubuntu:~$ echo $NAME
afei
afei@ubuntu:~$ echo \$NAME
$NAME
afei@ubuntu:~$ 

Tags: Linux Operation & Maintenance Ubuntu

Posted on Wed, 27 Oct 2021 08:41:32 -0400 by new7media