Shell Script Learning Finishing (i)

Source: Internet
Author: User

What is a shell script?

Shell script is similar to batch processing under Windows/dos, that is, the use of various commands pre-placed into a file, convenient one-time execution of a program file, mainly for the convenience of administrator settings or management. However, it is more powerful than batch processing under Windows and is more efficient than editing programs with other programming programs, and it uses commands under Linux/unix.

Initial knowledge of shell scripts

After you write a list of commands, you can press chmod +x test_8_8.sh or bash test_8_8.sh to run. (There are spaces after chmod)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/8E/wKiom1eoUJXwZ023AAAZdjtsvsc866.png "title=" Qq20160808160700.png "alt=" Wkiom1eoujxwz023aaazdjtsvsc866.png "/>

The Note:shell script line is a command without a semicolon.

After running, you can see the following results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/8D/wKioL1eoULjhX2t2AAAyhrHRMMs351.png "title=" Qq20160808160808.png "alt=" Wkiol1eouljhx2t2aaayhrhrmms351.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/8E/wKiom1eoUNexSUcBAAAI-o6u6kA632.png "title=" Qq20160808160835.png "alt=" Wkiom1eounexsucbaaai-o6u6ka632.png "/>

Shell Script Execution Process

(i) using chmod +x script file name

The shell will fork a child process and call the Exec system to invoke the Execute script file program. Because the script file is a text file without _start and code snippets, another mechanism for using exec is used. The first line of the text file specifies the interpreter, then replaces the current process with the code snippet of the interpreter program and executes from the _start of the interpreter code snippet and the text file is passed to the interpreter as a command-line argument. This process is actually equivalent to executing the program.

(ii) using the Bin/bash script file name

Bash first fork a child process shell to execute the script, the shell one line to read the script commands, and then fork a sub-process, the shell waits for each sentence after the execution of the next sentence, until the end of the file shell, bash wait for user input.

Shell variables and variable references

As a rule, shell variables consist of all caps and underscores, and there are two types of shell variables:

Environment variables-environment variables can be passed from the parent process to the child process, so the environment variables of the shell process can be passed from the current shell process tothe child process for K.  Use the PRINTENV command to display the environment variables for the current shell process.

Local variable--only exists in the current shell process, with the SET command to display all variables defined in the current shell process (including local variables and environment variables) and functions.

Environment variables are concepts that are present in any process, and local variables are shell-specific concepts. In the shell, environment variables and local variables are defined and used similarly.

Define or assign a variable to a shell:

value=100

Note: Be aware that there can be no spaces on either side of the equals sign, or the shell will be interpreted as command and command line arguments.

A variable is defined only in the current shell process, it is a local variable, the export command can be exported to the local variables as environment variables, defining and exporting environment variables can usually be completed in one step:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/8E/wKiom1eoUQrj1QaIAAATMyErhRs626.png "title=" Qq20160808165427.png "alt=" Wkiom1eouqrj1qaiaaatmyerhrs626.png "/>

If a variable is called VARNAME, it can be represented by ${varname}, and its value can be expressed in $varname without ambiguity.

Note: When defining a variable, do not use $ when taking the variable value. Unlike the C language, shell variables do not need to be explicitly defined, and in fact the values of the shell variables are strings. The shell variable does not need to be defined before it is used, and the value is an empty string if a variable is not defined.

command substitution and transfer characters

It is also a command that is enclosed in anti-quotes, which the shell executes first, and then immediately substitutions the output to the current command line.

Arithmetic substitution $ (())

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/8E/wKioL1eoUTTAGdKRAAAP3o0Orfs270.png "style=" float: none; "title=" Qq20160808171453.png "alt=" Wkiol1eouttagdkraaap3o0orfs270.png "/>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/8E/wKiom1eoUYaiNbPcAAALNqYJrx0190.png-wh_500x0-wm_3 -wmp_4-s_1479773897.png "title=" Qq20160808171437.png "alt=" Wkiom1eouyainbpcaaalnqyjrx0190.png-wh_50 "/>

Similar to the C language, \ is used as an escape character in the shell to remove the special meaning of a single character immediately following it (except for a carriage return), in other words, the literal value of the character immediately followed.

Create a folder named $ $

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/85/8E/wKiom1eoUaXyXDbRAAAPiZigMmI123.png-wh_500x0-wm_3 -wmp_4-s_3697342514.png "title=" Qq20160808171816.png "alt=" Wkiom1eouaxyxdbraaapizigmmi123.png-wh_50 "/>

There is a character that does not have a special meaning, but it is cumbersome to use it as a filename. If you want to create a file with a filename that begins with the-number, this is not possible: even adding \ Escaping is an error: because the various UNIX commands use the command-line arguments that begin with the-number as an option for the command, not as a file name. If you do not want to process a file name that begins with a-number, there are two ways to do this:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/8E/wKioL1eoUbyRKxEMAAAOHRpOa04456.png "title=" Qq20160808172025.png "alt=" Wkiol1eoubyrkxemaaaohrpoa04456.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/8E/wKiom1eoUc6gtYkIAAAN6zKZDgg710.png "title=" Qq20160808172054.png "alt=" Wkiom1eouc6gtykiaaan6zkzdgg710.png "/>

Single and double quotation marks

The single and double quotation marks in a shell script are the same as the delimiter of the string, not the character's delimiter. Single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but single quotes cannot appear in the string. If the quotation marks are entered without pairing, the shell gives a continuation prompt that asks the user to match the quotation marks. Double quotation marks are used to keep the literal value of all characters in the quotation marks (carriage return is no exception), with exceptions.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/8E/wKioL1eoUfDQKXsxAAAKnl9-Mos914.png "style=" float: none; "title=" Qq20160808172640.png "alt=" Wkiol1eoufdqkxsxaaaknl9-mos914.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/8E/wKiom1eoUfDieSaSAAANOYJfGcU853.png "style=" float: none; "title=" Qq20160808172652.png "alt=" Wkiom1eoufdiesasaaanoyjfgcu853.png "/>




This article is from the "July boreas" blog, please be sure to keep this source http://luminous.blog.51cto.com/10797288/1835741

Shell Script Learning Finishing (i)

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.