Get used to using the linux command line to manage linux systems, for example:
Copy the codeThe code is as follows:
$ date
2 11 23 01:34:58 CST 1999
$
When the user logs in, he actually enters the shell, which follows a certain syntax to interpret the entered commands and pass them to the system.
The first word entered in the command line must be the name of a command, the second word is the option or parameter of the command. Each word in the command line must be separated by a space or TAB, and the format is as follows:
Copy the codeThe code is as follows:
$ Command Option Arguments
1. Options and parameters
An option is a code that includes one or more letters, which is preceded by a minus sign (the minus sign is necessary, which Linux uses to distinguish options and parameters). The option can be used to change the type of actions performed by a command. For example:
Copy the codeThe code is as follows:
$ ls
motd passwd
motd passwd
This is the ls command without options, which lists all files in the current directory, only the names of each file, without showing other more information.
Copy the codeThe code is as follows:
$ ls -l
total 2
-rw-r--r-- 2 wzh book 22 Apr 20 20:37 motd
-rw-r--r-- 2 wzh book 796 Apr 20 20:37 passwd
total 2
-rw-r--r-- 2 wzh book 22 Apr 20 20:37 motd
-rw-r--r-- 2 wzh book 796 Apr 20 20:37 passwd
Adding the -l option will list a line of information for each file, such as the data size and the time when the data was last modified.
Most commands are designed to accept parameters. Parameters are one or more words typed after options on the command line, for example:
Copy the codeThe code is as follows:
$ ls -l text
-rw-r--r-- 2 wzh book 22 Apr 20 20:37 motd
-rw-r--r-- 2 wzh book 796 Apr 20 20:37 passwd
$
-rw-r--r-- 2 wzh book 22 Apr 20 20:37 motd
-rw-r--r-- 2 wzh book 796 Apr 20 20:37 passwd
$
All files and their information in the text directory will be displayed.
Some commands, such as ls, can take parameters, while some commands may require some minimum number of parameters. For example, the cp command requires at least two parameters. If the number of parameters does not match the command requirements, the shell will give an error message. For example:
Copy the codeThe code is as follows:
$ cp -i mydata newdata
Note: The options on the command line are entered before the parameter.
2. Command line features
The command line is actually a text buffer that can be edited, and the entered text can be edited before pressing Enter. For example, using the BACKSPACE key, you can delete the characters you just typed, delete the entire line, and insert characters, so that if a user enters a command, especially a complex command, there is no need to re-enter the entire command. Just use editing operations to correct the error.
The up arrow can re-display the command just executed. This function can repeatedly execute the commands that have been executed without re-typing the command.
bash saves a list of commands that have been typed before, which is called the command history table. Press the up arrow to display each command in succession on the command line. Similarly, pressing the down arrow can move down the command list, so that previous commands can be displayed on the command line, which the user can modify and execute. This feature will be discussed in detail in Section 10.4.
You can also place multiple commands in one command line, separating each command with a semicolon. For example:
Copy the codeThe code is as follows:
$ ls -F;cp -i mydata newdata
You can also enter a command in several command lines and use a backslash to continue one command line to the next line.
Copy the codeThe code is as follows:
$ cp -i /
mydata /
newdata
mydata /
newdata
The above cp command is entered in three lines. The two lines that start end with a backslash, and the three lines are used as a command line.
Special characters in shell
In addition to using ordinary characters, you can also use some special characters with special meanings and functions in the shell. When using them, you should pay attention to their special meaning and scope of action. These special characters are introduced below.
Three, wildcard
Wildcards are used for pattern matching, such as file name matching, path name search, string search, etc. Commonly used wildcard characters include *, ? and character sequences surrounded by square brackets [ ]. The user can include these wildcard characters in the file name as a command parameter to form a so-called "pattern string" to perform pattern matching during execution. The slashes (/) must be matched explicitly. For example, "*" cannot match .file, but ".*" can match .file.
? Represents any single character.
[ ] represents a specified character range. As long as the character at the position [ ] in the file name is within the range specified in [ ], the file name matches the pattern string. The character range in square brackets can be composed of characters given directly, or it can be composed of a starting character, a terminating character and a hyphen (-) representing a defined range. For example, f [a- d] has the same function as f [abcd]. The shell will use all file names matching the pattern string specified in the command line as parameters of the command to form the final command, and then execute this command.
The specific meaning of these wildcards.
Four, pattern string meaning
Copy the codeThe code is as follows:
* The names of all files in the current directory.
*Text* The names of all file names in the current directory containing Text.
[ab-dm]* The names of all files starting with a, b, c, d, and m in the current directory.
[ab-dm]? The names of all files in the current directory that start with a, b, c, d, and m and are followed by only one character.
/usr/bin/?? The names of all files with two characters under the directory /usr/bin.
Note that the hyphen "-" is only valid in square brackets, indicating the character range. If it is outside square brackets, it becomes a normal character. * and ? are wildcards only outside square brackets. If they appear inside square brackets, they will lose the ability of wildcards and become ordinary characters. For example, only one pair of brackets in the pattern "- a[*?]abc" is a wildcard, and both * and ? are normal characters, so the strings it match can only be - a*abc and - a?abc.
Finally, let me explain some issues that need to be paid attention to when using wildcards. Since *, ? and [ ] have relatively special significance for shells, these characters should not appear in normal file names. Especially don't appear in directory names, otherwise the shell may be infinitely recursive when matching. Another thing to note is that if there is no file name in the directory that matches the specified pattern string, then the shell will use this pattern string itself as a parameter to the relevant command. This may be the reason why special characters appear in the command.
Five, quotes
In the shell, quotes are divided into three types: single quotes, double quotes and backquotes.
Six, single quotes ‘
Characters enclosed in single quotes appear as normal characters. After special characters are enclosed in single quotes, they will lose their original meaning and are only interpreted as ordinary characters. For example:
Copy the codeThe code is as follows:
$ string='$PATH'
$ echo $string
$PATH
$ echo $string
$PATH
It can be seen that $ retains its own meaning and appears as a normal character.
Seven, double quotes “
Characters enclosed in double quotes, except for the characters $, /, ', and " are still special characters and retain their special functions, the rest of the characters are still treated as ordinary characters. For $, it is to replace the variable and $ with the value of the variable specified after it; for /, it is an escape character, which tells the shell not to specialize the character after it, and only treat it as a normal character. It can be imagined that in double quotes, there are only four characters that need to be added before /. For the "sign, if / is not added before it, the Shell will match it with the previous "sign.
For example, suppose the value of PATH is .:/usr/bin:/bin, enter the command:
Copy the codeThe code is as follows:
$ TestString=”$PATH///”/$PATH”
$ echo $TestString
.:/usr/bin:/ bin/”$PATH
You can try what results will be produced without adding/before the second double quote.
Eight, backticks `
The key corresponding to the character backtick (`) is generally located in the upper left corner of the keyboard, so don't confuse it with single quote ('). A string enclosed with backticks is interpreted as a command line by the shell. When executed, the shell first executes the command line and replaces the entire backtick (including two backticks) part with its standard output. For example:
Copy the codeThe code is as follows:
$ pwd
/home/xyz
$ string=”current directory is `pwd`”
$ echo $string
current directour is /home/xyz
When the shell executes the echo command, it first executes the command pwd in `pwd`, and replaces the output result /home /xyz with the `pwd` part, and finally outputs the replaced entire result.
The function of backticks can be used to replace commands, that is, the execution result enclosed by backticks is assigned to the specified variable. For example:
Copy the codeThe code is as follows:
$ today=`date`
$ echo Today is $today
Today is Mon Apr 15 16:20:13 CST 1999
Backticks can also be used in nesting. However, it should be noted that when nesting, the backticks on the inner layer must be escaped with a backslash (/).
For example:
Copy the codeThe code is as follows:
$ abc=`echo The number of users is /`who| wc-l/``
$ echo $abc
The number of users is 5
Special characters of shell can also be used in the command line between backticks. In order to get the result of the command in ``, the shell actually needs to execute the command specified in ``. When executed, special characters in the command, such as $, ", ?, etc., will have special meanings, and the `` can contain any legal shell command, such as:
Copy the codeThe code is as follows:
$ ls
note Notice
$ TestString=”`echo $HOME ` ` ls [nN]*`”
$ echo $TestString
/home/yxz note Notice