1. Special files:/Dev/null and/dev/tty
Linux provides two special files that are very useful for shell programming:/dev/null and/dev/tty. /Dev/null will discard all data written into it. In other words, when Program When writing data to this file, you will think that it has successfully completed the Data Writing operation, but nothing is actually done. This function is useful if you need to exit the command rather than output it. See the following shell Code :
/> VI test_dev_null.sh
#! /Bin/bash
If grep Hello testfile>/dev/null
Then
Echo "found"
Else
Echo "not found"
Fi
Save and exit in VI and run the following command:
/> Chmod + x test_dev_null.sh # Make this file an executable file
/> CAT> testfile
Hello my friend
CTRL + D # exit the command line file editing status
/>./Test_dev_null.sh
Found # The grep command execution result is not output here.
Modify the preceding shell script as follows:
/> VI test_dev_null.sh
#! /Bin/bash
If grep Hello testfile
Then
Echo "found"
Else
Echo "not found"
Fi
After saving and exiting in VI, run the script again:
/>./Test_dev_null.sh
The execution result of the hello my friend # grep command is output.
Found
Next let's take a look at the use of/dev/tty. When the program opens this file, Linux will automatically redirect it to a terminal window, so this file is particularly useful for reading manual input. See the following shell code:
/> VI test_dev_tty.sh
#! /Bin/bash
Printf "Enter New Password:" # prompt for Input
Stty-echo # disable the function of automatically printing input characters
Read Password </dev/tty # Read Password
Printf "\ nenter again:" # prompt again after line feed
Read password2 </dev/tty # Read again to confirm
Printf "\ n" # line feed
Stty echo # Remember to enable the function of automatically printing input characters
Echo "Password =" $ password # output read variable
Echo "password2 =" $ password2
Echo "all done"
Save and exit in VI and run the following command:
/> Chmod + x test_dev_tty.sh# Make this file an executable file
/>./Test_dev_tty
Enter New Password: # The password input is read into the password variable in the script.
Enter again: # Here, the password input is read into the password2 variable in the script.
Password = Hello
Password2 = Hello
All done
2. simple command tracking:
Linux Shell provides two methods to track commands in shell scripts to help us locate problems in the program accurately. The following code is the first method, which prints all executed commands in the shell script to the terminal and adds "+" before the command ": the plus sign is followed by a space.
/> CAT> trace_all_command.sh
Who | WC-L # The two shell commands will output the number of users logged on to the current Linux Server
CTRL + D # exit the command line file editing status
/> Chmod + x trace_all_command.sh
/> Sh-X./trace_all_command.sh # The-x option of the shell executor enables the script execution tracking function.
+ WC-L # Two shell commands tracked
+ Who
2 # actual output result.
Another method provided by Linux Shell is to print only some shell commands executed. This method is particularly useful when debugging complicated scripts.
/> CAT> trace_patial_command.sh
#! /Bin/bash
Set-X # enable the tracking function after this command
Echo 1st echo # shell command to be printed and Output
Set + X # The shell command will also be printed and output. However, after the command is executed, all commands will not be printed.
Echo 2nd echo # The shell command will no longer be printed.
CTRL + D # exit the command line file editing status
/> Chmod + x trace_patial_command.sh
/>./Trace_patial_command.sh
+ Echo 1st echo
1st echo
+ Set + x
2nd echo
Iii. Regular Expression basic syntax description :
In the Linux Shell environment, two regular expression rules are provided. One is the basic regular expression (BRE), and the other is the extended regular expression (ERE ).
The syntax list of these two expressions is as follows. Note that, if meta characters are not explicitly stated, they can be used for both bre and ere; otherwise, they will apply to the specified mode.
Regular metacharacters |
Meaning |
Use Cases |
\ |
It is usually used to disable the special meaning of subsequent characters and restore the original intention. |
\ (... \), The brackets here only represent parentheses. |
. |
Match any single character. |
A. B will match ABB, ACB, etc. |
* |
Match a single character between 0 and N. |
A * B will match AB, AAB, aaab, etc. |
^ |
Match the followed regular expression at the beginning of the row. |
^ AB, which matches ABC, Abd, and so on, but does not match cab. |
$ |
Matches the followed regular expression at the end of the row. |
AB $, will match AB, cab, etc., but does not match ABC. |
[...] |
Square brackets match any internal characters. -Indicates the range of consecutive characters. If the ^ symbol is placed in square brackets, the first character has a reverse meaning, that is, it matches any character not in the list (square brackets. If you want] and-to indicate the original intention, You need to place them at the first character position of square brackets, such as [] AB] or [-AB]. If both characters exist at the same time, place] at the beginning and end, for example, [] AB-]. |
[A-bA-Z0-9!] All uppercase/lowercase letters, numbers, and exclamation points. [^ ABC] indicates all characters other than A, B, and C. [TT] om, which can match Tom and Tom. |
\ {N, m \} |
An interval expression that matches the number of occurrences of a single character before it. \ {n \} indicates n times of repetition; \ {n, \} indicates at least N times of repetition; \ {n, m \} indicates repeated n to m times. |
AB \ {2 \} indicates abb; AB \ {2, \} indicates ABB and abbb. AB \ {2, 4 \} indicates ABB, abbb, and abbbb. |
\(...\) |
The mode between parentheses is stored in a special "reserved space ". A maximum of nine independent sub-modes can be stored in a single mode. Text that matches the sub-mode can be reused in the same mode through escape sequence \ 1 to \ 9. |
\ (AB \). * \ 1 indicates that the AB combination appears twice, and there can be any number of characters between the two, such as abcdab and Abab. |
{N, m} (ERE) |
The function is equivalent to the above \ {n, m \}, but the \ escape character is no longer written. |
AB + matches AB and abbb, but does not match. |
+ (ERE) |
Compared with the asterisk, + matches 1-N instances of the regular expression. |
|
? (ERE) |
Match the First Regular Expression with 0 or 1. |
AB? Only a or AB is matched. |
| (ERE) |
Match with | the regular expression before and after the symbol. |
(AB | cd) matches AB or CD. |
[: Alpha:] |
Match letter characters. |
[[: Alpha:]!] AB $ matches cab, dab, and! AB. |
[: Alnum:] |
Matches letters and numbers. |
[[: Alnum:] AB $ matches 1AB and AAB. |
[: Blank:] |
Matches spaces and tab characters. |
[[: Alnum:] AB $ matches 1AB and AAB. |
[: Cntrl:] |
Match control characters. |
|
[: Digit:] |
Match numeric characters. |
|
[: Graph:] |
Matches non-space characters. |
|
[: Lower:] |
Match lowercase letters. |
|
[: Upper:] |
Match uppercase letters. |
|
[: Punct:] |
Match punctuation characters. |
|
[: Space:] |
Matches whitespace characters. |
|
[: Xdigit:] |
Match the hexadecimal number. |
|
\ W |
Match any character consisting of letters and numbers, equivalent to [[: alnum:] _] |
|
\ W |
Match any character consisting of non-letters and numbers, equivalent to [^ [: alnum:] _] |
|
\ <\> |
Match the start and end of a word. |
\ <Read matches README, me \> matches readme. |
The following lists the types of Regular Expressions supported by common tools or commands in Linux Shell.
|
grep |
SED |
VI |
egrep |
awk |
Bre |
* |
* |
* |
|
|
Ere |
|
|
|
* |
* |
4. Use the cut command to select fields:
The cut command is used to cut down data in a text file. A text file can be of the field or character type. The following is an application example:
/> CAT/etc/passwd
Root: X: 0: 0: Root:/root:/bin/bash
Bin: X: 1: 1: Bin:/bin:/sbin/nologin
Daemon: X: 2: 2: daemon:/sbin/nologin
ADM: X: 3: 4: ADM:/var/adm:/sbin/nologin
......
/> Cut-D:-F 1,5/etc/passwd # The colon following-D indicates the delimiter between fields.-F indicates which fields are separated.
Root: Root # The first and fifth fields are retrieved here.
Bin: Bin
Daemon: Daemon
ADM: ADM
......
/>Cut-D:-F 3-/etc/passwd # Display from the third field until the last field.
0: 0: Root:/root:/bin/bash
1: 1: Bin:/bin:/sbin/nologin
2: 2: daemon:/sbin/nologin
3: 4: ADM:/var/adm:/sbin/nologin
4: 7: LP:/var/spool/lpd:/sbin/nologin
......
The cut command can also be used to cut part of the characters with the number of characters as the scalar. This function is implemented using the-C option and cannot coexist with the-D option.
/> Cut-C 1-4/etc/passwd # Use the first 1-4 characters of each line.
/> Cut-C-4/etc/passwd # Obtain the first 4 characters of each line.
Root
Bin:
DAEM
ADM:
......
/> Cut-C4-/etc/passwd # Take the 4th to the last character of each line.
T: X: 0: 0: Root:/root:/bin/bash
: X: 1: 1: Bin:/bin:/sbin/nologin
Mon: X: 2: 2: daemon:/sbin/nologin
: X: 3: 4: ADM:/var/adm:/sbin/nologin
......
/> Cut-C1, 4/etc/passwd # Obtain the first and fourth characters of each line.
RT
B:
DM
A:
......
/& Gt; cut-c1-4, 5/etc/passwd #1-4 and 5th characters in each line.
Root:
Bin: x
Daemo
ADM: x
5. Calculate the number of lines, characters, and characters:
Linux provides a simple tool WC to complete this function. See the following example:
/> Echo this is a test of the Emergency Broadcast System | WC
1 9 49 #1 line, 9 words, 49 characters
/> Echo testing one two three | WC-C
22 #22 characters
/> Echo testing one two three | WC-l
1 # Line 1
/> Echo testing one two three | WC-W
4 #4 words
/> WC/etc/passwd/etc/group # Calculate the data in two files.
39 71 1933/etc/passwd
62 906/etc/group
101 133 2839 total usage
6. Extract the number of rows starting or ending:
Sometimes, you need to extract several lines of words from a text file, most of which are near the beginning or end. Such as viewing work logs. Linux Shell provides the head and tail commands to complete this task. See the following example:
/> Head-N 5/etc/passwd# Display the first five lines of the input file.
Root: X: 0: 0: Root:/root:/bin/bash
Bin: X: 1: 1: Bin:/bin:/sbin/nologin
Daemon: X: 2: 2: daemon:/sbin/nologin
ADM: X: 3: 4: ADM:/var/adm:/sbin/nologin
LP: X: 4: 7: LP:/var/spool/lpd:/sbin/nologin
/> Tail-N 5/etc/passwd # Display the last five lines of the input file.
Sshd: X: 74: 74: privilege-separated SSH:/var/empty/sshd:/sbin/nologin
MySQL: X: 27: 27: MySQL Server:/var/lib/MySQL:/bin/bash
Pulse: X: 496: 494: pulseaudio system daemon:/var/run/pulse:/sbin/nologin
TPD: X: 42: 42:/var/lib/TPD:/sbin/nologin
STEPHEN: X: 500: 500: STEPHEN:/home/STEPHEN:/bin/bash
If you want to view the continuously increasing logs (such as the logs output by the Service Program), you can use the-F option of tail to prevent the tail command from automatically exiting, you must use the CTRL + C command to force exit. Therefore, this option is not suitable for shell scripts. See the following example:
/> Tail-F-N 5 my_server_log
......
^ C # Ctrl + C exit to the command line prompt state.