Script Programming--shell

Source: Internet
Author: User
Tags arithmetic terminates

One, Shell script

Pre-placed in a file with a variety of commands to facilitate one-time execution of a program text; some shell syntax and instructions are written inside, and then the functions such as formal notation, pipeline commands, and data flow redirection are used to achieve the desired processing purpose. The shell is a command-line interpreter whose function is to interpret the input commands and pass them on to the system, following a certain syntax. It provides users with an interface system-level program that sends requests to Linux to run programs, allowing users to start, suspend, stop, or even write programs with the shell.

Second, shell execution script

1. Touch "filename". SH, for example touch test.sh->vim test.sh, is written as follows:

1 #!/bin/bash 2 echo "Hello World" 3 CD ... 4 ls

The A.shell script uses # to denote comments, but if # is at the beginning of the first line, the exception is the script, which indicates that it interprets the execution using the interpreter specified later/bin/sh

B. Manner of implementation:

The first method of execution: chmod +x test.sh

Second mode of execution:/bin/bash test.sh

Third, shell execution process

1. Interactive shell (bash) fork/exec a child shell (SH) to execute a script, 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 current working directory of the file, SH waits for LS to terminate.

4, LS terminates, SH continues to execute, read to the end of the script file, SH terminates.

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

Four, shell variables

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

1. 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 to the fork

Out of the child process. Use the PRINTENV command to display the environment variables for the current shell process

2. Local Variables

exists only 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:

Varname=value

Note that there can be no spaces on either side of the equal sign, or it will be interpreted by the shell as command and command line arguments. A variable is defined only after

exists in the current shell process, which is a local variable that can be exported to an environment variable with the Export command

V. Variable references

If a variable is called VARNAME, it can be represented by ${varname}, and it can be VARNAME to represent its value without causing ambiguity. The differences between the two representations are compared by the following example:

echo $SHELLecho $SHELLabcecho ${shell}echo ${SHELL}ABC

Note that when you define a variable, you do not use $ when you take the variable value. Unlike the C language, shell variables do not need to be clearly defined

Type, in fact the value of the shell variable is a string, such as we define VAR=45, in fact, the value of Var is a string 45 instead of an integer. 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.

Vi. File name substitution

Wildcard *: matches 0 or more arbitrary characters

? : Matches an arbitrary character

[Several characters]: matches one occurrence of any character in square brackets

$ ls/dev/ttys*

$ ls ch0?. Doc

$ ls Ch0[0-2].doc

$ ls Ch[012][0-9].doc

Note that the file name that the globbing matches is expanded by the shell, which means that the parameters have been expanded before they are passed to the program.

, such as the above LS ch0[012].doc command, if the current directory has Ch00.doc and Ch02.doc, then the parameter passed to the LS command is actually the two file name, rather than a matching string.

Vii. command substitution: ' or $ ()

1, surrounded by anti-quotation marks is also a command, the shell first executes the command, and then immediately replace the output results to the current command line

Date= ' Date ' echo $DATE

For example, define a variable to hold the output of the DATE command: The command substitution can also be expressed in $ (): $ date=$ (date)

2. Arithmetic substitution: $ (())

For arithmetic calculations, the value of the shell variable in $ (()) is converted to an integer, for example:

Var=45 echo $ (($VAR +3))

Only the +-*/and () operators can be used in $ (()), and only integer operations can be done.

Eight, escape character \

1, such as creating a file named "$ $" (there are spaces in the middle) can be this:

$ Touch \$\ \$

2, for example, create a file named "-" to start the file can be like this:

Touch./-hello


Nine, single quotation marks

Unlike the C language, the single and double quotes in shell scripts are the same as the delimiter of the string, not the delimiter of the character. 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. For example:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/3C/wKiom1dtBhLCdmaUAABHgPtvT9c385.jpg-wh_500x0-wm_3 -wmp_4-s_2159526332.jpg "title=" single quotation mark. JPG "alt=" Wkiom1dtbhlcdmauaabhgptvt9c385.jpg-wh_50 "/>

Ten, double quotes

Double quotation marks are used to keep the literal value of all characters in quotation marks (carriage return is no exception), except in the following cases:

$ plus variable name to take the value of the variable

Anti-quotation marks still indicate command substitution

\$ represents the literal value of $

The literal value of \ ' representation '

The literal value of \ "represents"

\ \ denotes the literal value of \ In addition to the above, there is no special meaning in front of other characters, only the literal value:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/83/3C/wKiom1dtBnWRBSIDAADBcRQu7XE113.jpg-wh_500x0-wm_3 -wmp_4-s_3150732610.jpg "title=" double quotation marks. JPG "alt=" Wkiom1dtbnwrbsidaadbcrqu7xe113.jpg-wh_50 "/>



Script Programming--shell

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.