A, Shell Scripting

Using the shell of a function written in plain text program, extension sh (sh behalf shell), the extension does not affect the execution of the script

Some of the instructions written on the inside of the shell, and then apply regular expressions important functions, the pipeline commands, data traffic redirection, arrays, loops, and the condition judgment logic judgment, so as to achieve the purpose of treatment we want.

Common script: shell scripts, Java script, PHP script, Python scripts.

Use Shell script?

Automatic installation, automatic management

clip_image002

How to execute the script file?

1. First create a script file script.sh, extension is .sh (.sh mainly to facilitate recognition This is a shell script file)

clip_image003

2. Write your Shell script in a file, save and exit

clip_image004

3. Add the executable permission to the script

clip_image005

4. Perform

<. 1> ./script.sh     Enter

<2> SH script.sh    Enter

<3> Specify the absolute path of the script file, you can perform

clip_image006

What difference does it make in the implementation of these two ways?

The first way, it defaults to / bin / sh points to shell interpreter to execute the script file, if the script file is not specified interpreter. But what if you specify the use of script interpreter in the script file, then it regardless of / bin / sh point to which script interpreter, but the use of the script interpreter script file enacted. Specifies the script interpreter in the script file:! # / Bin / bash

The second way, using / bin / sh points to shell interpreter to execute the script file

The third way, choosing the script interpreter and the second one and the same.

Second, variable

Variable is used to store values ​​of non-fixed vector, which has a value, and zero or more attributes.

1. Custom Variables

Non-interactive definition of variables:

clip_image008

Use <$ variable name> to call a variable value

Interactive definition of variables:

clip_image009

2. Position Variables

So that the command in a script parameter when you run the script in different locations can call.

· $0 当前shell程序的文件名

· $1 运行shell的第一个参数

· $# shell程序所有参数的个数

· $* 所有参数的内容,将参数作为一个整体

· $@ 所有参数的内容,将参数分别作为个体看待

· $$ 当前进程的ID

· $? 程序退出的代码

clip_image010clip_image011

3. 环境变量

Linux是一个多用户的操作系统,每个用户登录系统时都会有一个专用的运行环境,通常情况下每个用户的默认的环境都是相同的。这个默认环境就是一组环境变量的定义。每个用户都可以通过修改环境变量的方式对自己的运行环境进行配置。

clip_image012

clip_image013

4. 展开替换

• 变量的展开替换

• 对变量的值做修改后输出

例:取绝对路径的文件名(目录名)

○ ${variable#key} 从头开始删除关键词key,最短匹配

○ ${variable##key} 从头开始删除关键词key,最长匹配

○ ${variable%key} 从尾开始删除关键词key,最短匹配

○ ${variable%%key} 从尾开始删除关键词key,最长匹配

○ ${variable/old/new} 用new来替换old,仅替换第一个

○ ${variable//old/new }用new来替换old,替换所有old

Ø ${#var}返回变量var的长度

Ø ${var:3}提取位置3到最后的字符

Ø ${var:3:5}从位置3开始提取5个字符

三、 Shell引号的规则

单引号

可将它中间的所有任意字符还原为字面意义

Double quotes

Any character in the middle of it all can be reduced to the literal sense, but does not block `, \, $

Backticks

Backticks command substitution, command character can be replaced with the results of the command, can also use the $ () to achieve