Basic learning of shell scripts

Source: Internet
Author: User

I. The history of the shell

The role of the shell is to explain the execution of the user's commands, the user enters a command, the shell interprets the execution of a bar, which is called Interactive.

The user also has a way of executing a command called batching, in which the user writes a shell script, which has many commands that allow the shell to execute the commands at once without having to strike a single command.

Shell scripts are similar to programming languages, and there are variables and process control statements, but shell scripts are interpreted and do not need to be compiled, and the shell reads and executes the commands from a single line of script, which is equivalent to a user knocking a line of commands from a script to the shell prompt for execution.

Two. How the shell executes

Due to historical reasons. There are many kinds of shells in Unix systems, such as Sh,csh,ksh,tcah,bash.

For the current shell, such as bash, when executing a command, it does not execute the command in person, because it is possible that this command is a malicious command that can have unintended consequences. So it creates a child process that lets the child process execute the shell command, which is also a bash, so when it executes a line of commands in a shell script, You also need to create a sub-process, which can be considered a child process, to execute a line of commands in a shell script.

Three. Shell execution script

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/9F/wKioL1d4whnQD3exAAAPjpin3NU110.png "title=" Shell0.png "alt=" Wkiol1d4whnqd3exaaapjpin3nu110.png "/>

The notation in the shell script is equivalent to//in the C language, but if # is at the beginning of the first line, it means that the script interprets execution using the interpreter specified later/bin/sh.

(1) chmod u+x script.sh

Because shell scripts do not need to be compiled, the shell script file that you just created does not have executable permissions, and you must add executable permissions to execute it.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/A0/wKiom1d4w6XQrEkPAABVKUjEb4A993.png "title=" Shell1.png "alt=" Wkiom1d4w6xqrekpaabvkujeb4a993.png "/>

(2)/bin/bash script.sh

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/9F/wKioL1d4xEKTNz8TAAAVTcdPQuQ051.png "title=" Shell2.png "alt=" Wkiol1d4xektnz8taaavtcdpquq051.png "/>

(3) In addition to the top two ways, there are several:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/A0/wKiom1d4xK3xbJqaAAAnLZij3fI374.png "title=" Shell3.png "alt=" Wkiom1d4xk3xbjqaaaanlzij3fi374.png "/>

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 forked child process. Use the PRINTENV command to display the environment variables for the current shell process.     

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/A0/wKiom1d4xpuw_moZAAB5Smd5br4588.png "title=" Shell4.png "alt=" Wkiom1d4xpuw_mozaab5smd5br4588.png "/> (2) Local variables

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

environment variables are concepts that are present in any process, while local variables are concepts that are specific to shell processes.

Define or assign a variable to a shell:

Varname=value

Note: There can be no spaces on either side of the equal sign, or it will be interpreted by the shell as a command or command line argument. 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: Export varname=value

can also be done in two steps: varname=value

Export VARNAME

Use the unset command to delete a locally variable or environment variable that has already been defined: unset VARNAME

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/9F/wKioL1d4yo-BL9jKAACd9aJo-Bw513.png "title=" Shell5.png "alt=" Wkiol1d4yo-bl9jkaacd9ajo-bw513.png "/>

Five. Variable references

If a variable is called VARNAME, ${VARNAME} can represent its value and can be expressed in $VARNAME without ambiguity. Use the following example to compare the differences between the two:

Program code:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/A0/wKiom1d4zOrghOrAAAAeBBaATdE940.png "title=" Shell7.png "alt=" Wkiom1d4zorghoraaaaebbaatde940.png "/>

Implementation results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/9F/wKioL1d4zPrizhnYAAAVLls6fqQ397.png "title=" Shell6.png "alt=" Wkiol1d4zprizhnyaaavlls6fqq397.png "/>

Note: You do not need $ when you define a variable, you use $ when you take the variable value.

Unlike the C language, a shell variable does not need to explicitly indicate its type. in fact, the value of the shell variable is a string , such as var=100, which indicates that the value of VAR is string 100 instead of integer 100. 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.

Six. The difference between the anti-quote "' and $ () and the contact

(1) The same point: all the enclosed commands are executed and given to the corresponding object or output.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/9F/wKioL1d40c7Ce5YVAAAQJwyAtrA488.png "title=" Shell9.png "alt=" wkiol1d40c7ce5yvaaaqjwyatra488.png "/> 650" this.width=650; "src=" http://s5.51cto.com/wyfs02/ M00/83/9f/wkiol1d40ewjsttvaaapi_ukdby706.png "title=" Shell8.png "alt=" Wkiol1d40ewjsttvaaapi_ukdby706.png "/>

(2) different points: first of all, to enumerate a simple example

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/9F/wKioL1d402uCEX3nAAAZp9SgOd0974.png "title=" Shell2.png "alt=" wkiol1d402ucex3naaazp9sgod0974.png "/> 650" this.width=650; "src=" http://s1.51cto.com/wyfs02/M0 1/83/a0/wkiol1d403qhcbjtaaazfsrs_2s607.png "title=" Shell1.png "alt=" Wkiol1d403qhcbjtaaazfsrs_2s607.png "/>

we can conclude by:

1. The \$ in the inverted quotation mark does not convert the special meaning of $ , the contents of the inverted quotation mark are still interpreted as an echo $VAL takes the value of the Val variable and outputs it.

  1. 2.$ () on the contrary, $ is obviously escaped to a normal character, so it does not take the value of the variable, but returns the meaning of the string itself, and returns the $val.

  2. This means that the anti- quote ' ' has a special conversion to the backslash, so that the backslash does not escape the effect . We make the following changes to the above procedure:

  3. 650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/A1/wKiom1d41iHDZaqmAAAbTJQgG-U838.png "title=" Shell4.png "alt=" wkiom1d41ihdzaqmaaabtjqgg-u838.png "/> 650" this.width=650; "src=" http://s3.51cto.com/wyfs02/ M02/83/a1/wkiom1d41i7tft71aaao_jyu8hq405.png "title=" Shell3.png "alt=" Wkiom1d41i7tft71aaao_jyu8hq405.png "/>

  4. Thus, the following results:

  5. 1. The anti-quote itself escapes the backslash and retains its own meaning, and if we think about the function of the counter-quote, we need to use \ \.

  6. 2.$ () does not need to consider the problem, the same as the usual use.


This article is from the "Zwy" blog, make sure to keep this source http://10548195.blog.51cto.com/10538195/1795379

Basic learning of shell scripts

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.