Shell Scripting related issues

Source: Internet
Author: User
Tags terminates

Shell: Explain the execution of user input commands, row by line interpretation, can also batch many instructions, you can write all these instructions into a script file, let the shell execute multiple commands at once, the common shell (bash, it takes the role of Shell, shell script interpreter, As an executable binary file)

Shell execution Process:

After the user enters a command at the command line, the shell typically fork the child process and call exec to replace it, letting its grandchild process execute the command, but with the exception of the shell's built-in command, the execution of the built-in command is equivalent to invoking a function in the shell process and does not create a new process. Commands such as CD, Export, Umask, exit, etc. are built-in commands

Specific steps:

1. Interactive shell (bash) fork/exec a child shell (SH) is used to execute the script, and the parent process bash waits for the child process sh to terminate.

2.SH read the CD in the script: command, call the appropriate function to execute the built-in command, changing the current working directory to the previous level.

3.sh read the LS command in the script, fork/exec This program, lists the files under the current working directory, SH waits for LS to terminate.

After the 4.ls terminates, SH continues to execute, reading to the end of the script file, SH terminates.

After the 5.sh terminates, bash resumes execution, and the print prompt waits for user input.

eval: is a normal command line typed on the terminal. However when placed in front of it eval shell scan it two times before executing the command line

Usage: eval [parameter] eval can read a series of parameters and then execute according to the characteristics of the parameters themselves.
Parameter: The parameter line is scanned two times before it is evaluated again to find out the contents of the parameter.

1. For echoing simple variables:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/B0/wKioL1deiqixjV8EAABKb90aP8g833.png-wh_500x0-wm_3 -wmp_4-s_4184545471.png "title=" 1.png "alt=" Wkiol1deiqixjv8eaabkb90ap8g833.png-wh_50 "/>


2. Two scans for complex variable display, create a new text file to display the contents of the text file

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/B1/wKioL1dejnqD1_b2AAAsWUdzBU0930.png-wh_500x0-wm_3 -wmp_4-s_2503115534.png "title=" 2.png "alt=" Wkiol1dejnqd1_b2aaaswudzbu0930.png-wh_50 "/>


3. The last parameter can be obtained

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/B3/wKiom1dekKXQY788AAAs3sb4gf4492.png-wh_500x0-wm_3 -wmp_4-s_3423849439.png "title=" 3.png "alt=" Wkiom1dekkxqy788aaas3sb4gf4492.png-wh_50 "/>


4. Create a pointer variable with eval, or change the contents of the space that the pointer refers to

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/B1/wKioL1dek8bTd1hQAAAwS88YuFw306.png-wh_500x0-wm_3 -wmp_4-s_1541057959.png "title=" 4.png "alt=" Wkiol1dek8btd1hqaaaws88yufw306.png-wh_50 "/>


eval: can not get the result of function processing, all commands, the function of processing results can only be obtained by ". Eval nesting is meaningless, and in other languages it can be performed (by eval ("code")) to perform (performing a dynamically generated code return), whereas in the shell the eval command is simply executed as a command string, and the nested effect is lost. Nesting is replaced by command substitution.


"and $ () Difference and contact

1. In some cases they are used the same, but there are differences between them

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/82/B3/wKiom1delVrD0AjfAABLH7LgwGg190.png-wh_500x0-wm_3 -wmp_4-s_2930205728.png "title=" 6.png "alt=" Wkiom1delvrd0ajfaablh7lgwgg190.png-wh_50 "/>

As shown in the example above, the values they get when displaying the current time are the same, but when the current directory is located in the home directory, "the special meaning of $ is converted, and the use of $ () is not explained, is \ escaped into a normal character


2. If you give the above example plus a/

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/B3/wKiom1del2GRnva7AAAug_WFNHs092.png-wh_500x0-wm_3 -wmp_4-s_2784784833.png "title=" 7.png "alt=" Wkiom1del2grnva7aaaug_wfnhs092.png-wh_50 "/>

$ () is escaped into a string, and the contents are interpreted inside

3. Write the following in the test.sh script

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/B3/wKiom1demP_Sba1AAAA5ATqXisU343.png-wh_500x0-wm_3 -wmp_4-s_2354209904.png "title=" 9.png "alt=" Wkiom1demp_sba1aaaa5atqxisu343.png-wh_50 "/>

As above, four backslashes are interpreted as one when using anti-quotes, and $ () is interpreted as two

4. When using multiple backslashes

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/B3/wKiom1deme2gBx25AAA9agiQW_4323.png-wh_500x0-wm_3 -wmp_4-s_3175288603.png "title=" 10.png "alt=" Wkiom1deme2gbx25aaa9agiqw_4323.png-wh_50 "/>

From above: The anti-quote itself is escaped to the \, preserving its own meaning, if you want to play in the anti-quotation mark in the special meaning, must use two \ means, $ () when used as usual \=\


[Test command and []] the difference and contact

1.[is an executable program

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/B1/wKioL1deoUGjZf6XAABZMjlwkIU716.png-wh_500x0-wm_3 -wmp_4-s_4114669933.png "title=" 11.png "alt=" Wkiol1deougjzf6xaabzmjlwkiu716.png-wh_50 "/>

2. Single brackets The Test command is to separate the word from the variable, enclose the variable in quotation marks when the value of the variable contains a white space character, and the double-brace Test command does not separate the word from the variable

When it comes to testing, Bash responds with too many parameters because the single quote test command separates the word from the variable, and the result becomes

[Hello world = ' Hello World '], with that string hello, a comparison between the string world and the string Hello World. So if the variable in the single-brace test command contains a space, but it also has to be compared to a string, then you have to add a double quote to the variable, and it will not have an error. The variable word cannot be separated by the test command in the following double parenthesis

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/B1/wKioL1deo7TA-CJ6AABU3C_2S1E466.png-wh_500x0-wm_3 -wmp_4-s_323741303.png "title=" 12.png "alt=" Wkiol1deo7ta-cj6aabu3c_2s1e466.png-wh_50 "/>

3. The test command for the parentheses is incorrectSHELLmeta-characters are extended, while the doublebracketsThe Test command willSHELLmeta-characters are expanded.

4. In the double-brace Test command, if a string (with or without a space) is simply the expression as a normal string, not part of a pattern, it must also be enclosed in quotation marks. If a string value (the one on the right) is not enclosed in double quotes, the string is a pattern, and if it contains SHELL metacharacters, bash expands it. If the string is double-quoted, it is a very ordinary string, even if the string contains special characters, that is, when the ordinary content to deal with.

5.[Express1–a Express2] This is the single-parenthesis test command , because the meta-character extensions are not supported, so they are only called expressions, and they can be combined to form the logical test



Shell Scripting related issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.