Summary of shell 13, Summary of shell 13
We know that computer hardware resources, such as disks, I/O, and memory, are centrally managed by software. Such special software is often referred to as an operating system, windows builds its own interface on the basis of underlying resource control, which is very suitable for use and can complete the functions we need just a little bit more. This is a way to control resources, and you can also use command to operate on underlying resources. The most important part of liunx is its kernel, which manages system resources and also provides us with an interface for operating the kernel, which is the shell we often use ), there are several mainstream shell types:
Sh:
Burne shell (sh)
Burne again shell (bash)
Csh:
C shell (csh)
Tc shell (tcsh)
Korn shell (ksh)
Bash and ksh are widely used. Several shell functions are similar and have their own characteristics.
Generally, $ is displayed for non-administrator users and root is displayed for administrators. In linux, the line break (Press enter) is represented by CR. In case of CR, the shell interpreter will explain the command line before CR, the command line is to split the command line according to IFS and then process the metacharacters. For example, in echo $ A, the value of A is hello, and the value of IFS (tab by default, newline, space) is split into echo and $ A, then the metacharacters $ are interpreted, the value of variable A is taken, and finally hello is output to the screen.
1. echo
Echo commands are commonly used output commands. You can view the parameter values. Several parameters are provided to achieve different effects. For example, echo-n is used to cancel the line break at the end, you can try to output echo-n "a" and echo "a" in the environment. If we want to add another line feed by default, we may think of echo "a \ n". This is not feasible. echo runs the-E parameter by default, it indicates that the backslash character conversion is not interpreted. Therefore, you must enable the \ function and use the-e parameter as follows: echo-e "a \ n ".
Ii. Double quotation marks "" and single quotation marks''
In shell, double quotation marks and single quotation marks have different functions. shell characters can be divided into two types: common characters and metacharacters. For example, $ is a metacharacters, when the metacharacters are executed, shell performs the corresponding action. For example, echo $ A prints the value of variable, but if we want to print "$ A", how should we implement it? The answer is to use single quotes and echo '$ A'. All metacharacters in single quotes will be treated as common characters, what is the difference between double quotation marks? Double quotation marks will disable most metacharacters, but a small part will still be interpreted as metacharacters, such as the $ symbol, another way to disable metacharacters is to add/backslash before the metacharacters.
Iii. var = value and export
Strictly speaking, all the variables defined in the current shell are local variables. Only the export is used as the environment variable for subsequent commands. The detailed explanation of the export command will be mentioned later.
Iv. exec and source
Source * is often used for shell execution *. sh. In fact, we execute *. sh script will generate a subshell process for execution, and the environment variables of the child process and the environment variables of the parent process are not shared, therefore, if we define the cd/home/a/B/d command in test. sh script, and run test under/home/. sh. In fact, the directory does not switch to/home/a/B/d. That's why. Then how can we execute test. sh enables directory switching. shell provides two methods to prevent the generation of subshell process execution scripts, source and exec. These two Commands will call the script in the current shell process. What is the difference between the two methods? Let's take a look at the following example:
1. sh
#!/bin/bashA=Becho "PID of 1.sh before exec/source/fork:$$"export Aecho "1.sh :\$A is $A"case $1 in exec) echo "using exec..." exec ./2.sh ;; source) echo "using source" . ./2.sh ;; *) echo "using fork" ./2.sh ;;esacecho "PID of 1.sh after exec/source/fork:$$"echo "1.sh:\$A is $A"
2. sh
#!/bin/bashecho "PID for 2.sh is: $$"echo "2.sh get \$A=$A form 1.sh"A=Cexport Aecho "2.sh:\$A is $A"
Result After./1.sh fork is executed:
PID of 1.sh before exec/source/fork: 18885
1. sh: $ A is B
Using fork
PID for 2.sh is: 18886
2. sh get $ A = B form 1.sh
2. sh: $ A is C
PID of 1.sh after exec/source/fork: 18885
1. sh: $ A is B
Result After./1.sh exec is executed:
PID of 1.sh before exec/source/fork: 20952
1. sh: $ A is B
Using exec...
PID for 2.sh is: 20952
2. sh get $ A = B form 1.sh
2. sh: $ A is C
Result After./1.sh source is executed:
PID of 1.sh before exec/source/fork: 26519
1. sh: $ A is B
Using source
PID for 2.sh is: 26519
2. sh get $ A = B form 1.sh
2. sh: $ A is C
PID of 1.sh after exec/source/fork: 26519
1. sh: $ A is C
The printed results are analyzed one by one as follows:
By default, fork execution generates a sub-process to execute shell scripts. The export effect in the parent process is passed to the sub-process, but the export effect in the sub-process is not passed to the parent process, therefore, the export command is one-way to pass sub-processes from the parent process.
Execute exec and source without generating sub-processes. Instead, execute the script in the current process. The difference is that exec terminates the execution of the current script. After the sub-script is executed, the entire execution process is complete.
V. Differences between () and {}
The concept of a command group should be introduced here, like functions in other languages. The difference between () and {} Is that () is executed in the sub-shell, {} is executed in this shell. I have to propose a concept here. There are two methods for defining functions in shell: function_name {}, another type is function_name (){}.
6. Differences between $ (), $ {}, and $ ()
In bash, $ () and ''are both used as command replications. Command replications are similar to the previous variable replications, such as dir = $ (pwd ), first, the execution result of pwd is assigned to the variable dir. $ {} is used to replace the variable, for example, A = B; echo $ {}, in fact, we can also directly echo $ A here, but if we write echo $ AA, shell does not recognize anything, so $ {} serves as A boundary, however, if you only think that $ {} serves as a boundary, it would be too small.
Assume there is a variable file =/dir1/dir2/dir3/my.file.txt.
We can use $ {} to replace different variables to get different values.
$ {File # */}: Remove all the characters on the first/and its left. The result is dir1/dir2/dir3/my.file.txt.
$ {File ### */} remove all the values on the last/and its left side, and the result is my.file.txt.
$ {File %/*} removes all the characters on the last/and its right. The result is/dir1/dir2/dir3.
$ {File %/*}: remove the first/and its right string: (null)
# Remove the left, % remove the right, one is the shortest match, and the other is the longest match
$ {File: 0: 5} is returned, which is 5 characters to the right starting from 0th./dir1
Therefore, $ {file: offset: length} is a string of length starting from offset to the right.
$ {File/dir/path}: Replace the first dir with path. The result is/path1/dir2/dir3/my.file.txt.
$ {File // dir/path} replaces all dir with path.
The above is a replacement of characters. You can also replace it based on whether the current value is null, as shown below:
When using file-myfile.txt.pdf if not specified, myfile.txt is output.
$ {File: -myfile.txt.pdf if the parameter is not specified as null, myfile.txt is output.
When file.txt is specified, myfile.txt is output.
$ {File: Specify myfile.txtfolder. If the file is not empty, myfile.txt is output.
When using file.txt myfile.txt.pdf if the filepath is not specified, the output myfille.txtat the same time sets the value filepath to myfile.txt.
$ {File: Specify myfile.txtworkflow. If the file is not specified as empty, myfile.txt is output.
Length of the output file variable $ {# file}
By the way, we will introduce the array in bash.
The array is defined as follows: A = (a B c d e) is separated by spaces.
To get the content of this array, use $ {A [@]} or $ {A [*]}.
Finally, let's take a look at the usage of $ (). $ () facilitates integer calculation, for example, a = 1; B = 2; echo $ (a + B) outputs 3. $ () Provides a C-like style for comparison and computation.
7. Differences between $ @ and $ *
For example, we have a sh script named test. sh. It can accept the following parameters: test. sh a B c. obtain external parameters by $1 $2 $3 respectively. If you want to obtain the name of the foot, use $0. Note that the functions defined in the script can also receive parameters. Similarly, you can use $1... to obtain the passed parameters. $0 is not the name of the function, but the name of the outermost script.
$10 indicates not obtaining the tenth parameter, but adding a string 0 after obtaining the first parameter. Therefore, to obtain the tenth parameter, use ${10 }, you can also use the shift command to remove the first parameter ($0) and shift 5 to cancel the first five parameters.
Another parameter $ # is used to obtain the total number of input parameters. For example, for test. sh one two three, $ # is 3.
If you want to obtain all the parameters, you can use $ @. For example, test. sh one two three gets three words, while $ * regards the parameters as a whole.
8. Difference Between & and |
Before introducing $ and |, let's take a look at a new concept: return value. When executing command or function, it will pass the value to the parent process. We can use $? To obtain the latest return value. The return value has only two States. If the return value is 0, the return value is true. If the return value is not 0, the return value is false. For example:
Assume that myfile.txtexists, and a.txt does not exist. After ls a.txt, echo $? It is not 0. If ls myfile.txt is 0. You can use test expersion or [expersion] to test the return value. However, I prefer the [] syntax.
Next, let's take a look at the difference between $ and |. command1 & command2 indicates that if command1 returns true, command2 is executed. Command1 | command2 indicates that if command1 returns false, command2 is executed.
IX. <and>
There are three types of FD (file descriptor): 0 standard input, 1 standard output, and 2 error output.> indicates that the standard output redirection is the same as 1>.
2> & 1 indicates redirecting the standard error output to the standard output.
&> File: redirects the standard output and standard error output to the file.
2> &-indicates that the standard error output is disabled.
>/Dev/null 2> & 1 indicates that the standard error output is bound to the standard input and the standard input is redirected to/dev/null, that is, nothing is output.
10. case and if
Sometimes different logic needs to be executed based on the current value. if is used here, the standard if usage is:
If comd1; then
Comd2
Elif comd3; then
Comd4
Else
Comd5
Fi
If you do not want to run any command after then, use the null command instead.
If there are many conditions and are strings, you can use another syntax, case
Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Restart | reload)
Restart
;;
*)
Exit 1
Esac
11. for, while, and
Loop is a very basic logical control. All Languages provide their own loop control statements.
For loop reads values from a list and processes them in sequence:
For var in one two three four five
Do
...
Done
The following is a statement for accumulating variables:
For (I = 1; I <= 10; I ++ ))
Do
...
Done
If it is changed to while, the implementation is as follows:
Num = 1
While [$ num-le 10]; do
...
Done
-Le indicates less than or equal
While indicates that the loop is input when the condition is true, while until indicates that the loop is input when the condition is false.
The break and continue keywords also exist in the same loop. They are used in the same way as other languages such as java, so they will not be explained.
The above is the general content of shell 13, which is not involved in some content. In the future, we will briefly explain some commonly used commands such as grep, sed, and awk.