Shell Scripting specification and variables

main content

Shell Scripting specification

  • Shell script scenarios
  • Shell Programming Specification
  • Pipes and redirection

Shell script variables

  • Custom Variables
  • Special Variables

Shell Script Overview

Shell script concept

  • The command to be executed in order to save a text file
  • Executable permissions to the file, you can run
  • Shell various control statements may be combined to perform more complex operations

Shell script scenarios

  • Repetitive operations
  • Batch Transactions
  • Automated operation and maintenance
  • Service health monitoring
  • Timing task execution

Shell first to write a script

Writing the script code

Use a text editor vim, one per line Linux commands executed in the order written:

[root@localhost ~]# vim first.sh
cd /boot/                                               //切换目录
pwd                                                      //显示当前所在目录
Is -Ih vml*                                            //查看所有“vml”开头的文件

2. given executable permissions

There are three general file permissions, read (r), write (w), execute (x). Shell scripts are usually written does not have execute (x) permission, so we need to give it execute permissions.

[root@localhost ~]# chmod +x first.sh                   //只需用chmod +x 命令后面接Shell脚本名即可
[root@localhost ~]#

3. Execute the script file

Method One: ./+ script file path (must execute privileges to perform, finished will not change the current directory)

[root@localhost ~]# ./first.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月   9 23:19 vmlinuz-0-rescue-41b16d21413d4ea6b21c13fddca8e20a
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# 

Method two: + script file path (do not need to execute permissions can execute)

[root@localhost ~]# . first.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月   9 23:19 vmlinuz-0-rescue-41b16d21413d4ea6b21c13fddca8e20a
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]#

Method three: SH + script file path (do not need to execute permissions will be able to perform, finished will not change the current directory)

[root@localhost ~]# sh first.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月   9 23:19 vmlinuz-0-rescue-41b16d21413d4ea6b21c13fddca8e20a
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# 

Method four: Source + script file path (do not need to execute permissions can execute)

[root@localhost ~]# source first.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月   9 23:19 vmlinuz-0-rescue-41b16d21413d4ea6b21c13fddca8e20a
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]#

4. constitute a perfect script

A perfect Shell scripts, also need to have scripting statement, annotation information, executable statement

[root@localhost ~]# vim first.sh 
#!/bin/bash                                      //脚本声明,告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。
#This is first script                         //注释信息,说明脚本的作用
cd /boot/
echo "当前的目录位于:"           //输出友好提示信息
pwd
echo "其中以vml开头的文件包括:"
ls -lh vml*

After the complete execution of the script again, the effect is as follows:

[root@localhost ~]# ./first.sh 
当前的目录位于:
/boot
其中以vml开头的文件包括:
-rwxr-xr-x. 1 root root 5.7M 8月   9 23:19 vmlinuz-0-rescue-41b16d21413d4ea6b21c13fddca8e20a
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# 

Redirection and pipeline operations

1. interactive hardware

Standard Input: data input from a user of the receiving apparatus;

Standard Output: output data to the user through the apparatus;

Standard Error: execution error report information through the device.

Types of Device file File Description Number Default Device
Standard Input /dev/stdin 0 keyboard
Standard output /dev/stdout 1 monitor
Standard error output /dev/stderr 2 monitor

2. redirection

Shell Scripting specification and variables

Simple redirect input presentation:

[root@localhost opt]# cd /opt/
[root@localhost opt]# touch aaa.txt bbb.txt
[root@localhost opt]# ls
aaa.txt  bbb.txt  rh
[root@localhost opt]# tar czvf test.tar.gz *.txt > status.txt           //创建压缩包,将输出结果保存到status.txt文件
[root@localhost opt]# ls
aaa.txt  bbb.txt  rh  status.txt  test.tar.gz
[root@localhost opt]# cat status.txt                                             //查看status.txt文件
aaa.txt
bbb.txt
[root@localhost opt]#

Stderr demo:

[root@localhost opt]# tar jxvf test.tar.gz -C /opt/ 2>> status.txt            //用错误的格式解压之前创建的压缩包,并将输出的错误信息追加到status.txt文件中
[root@localhost opt]# ls
aaa.txt  bbb.txt  rh  status.txt  test.tar.gz
[root@localhost opt]# cat status.txt                                              //查看status.txt文件,可以看到追加的错误信息
aaa.txt
bbb.txt
bzip2: (stdin) is not a bzip2 file.
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@localhost opt]# 

Other I do not do presentations, and similar.

3. The pipeline operator symbol "|"

The left side of the output commands as command on the right processed
command format:cmd1 | cmd2 [.. I cmdn]

[root@localhost ~]# grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
jiang:x:1000:1000:jiang:/home/jiang:/bin/bash
[root@localhost ~]# grep "bash$" /etc/passwd | awk -F: '{print $1,$7}'
root /bin/bash
jiang /bin/bash
[root@localhost ~]#

The role of Shell variables, type

1. The role of variables

Provide specific parameters for the flexible management of Linux systems, it has two meanings.
Variable name: fixed name preset by the system or user defined;
variable values: can be set by the user varies depending on the system environment.

2. The type of variable

Custom Variables: defined by the user, modification and use;
environment variables: maintained by the system, for setting the working environment;
position variables: passing parameters to the script program from the command line;
predefined variables: the Bash built into a class variable, You can not be modified directly.

Custom Variables

1. Define a new variable

Variable names begin with a letter or an underscore English case-sensitive, the first character can not start with a number. Excluding space, you can use the underscore,
you can not use punctuation, you can be used in bash keyword (help command to view the available reserved keywords).

Format: variable name = variable value

[root@localhost ~]# Product=Python
[root@localhost ~]# Version=2.7.13
[root@localhost ~]# 

2. Check the value of the variable

Format: echo $ variable name

[root@localhost ~]# echo $Product 
Python
[root@localhost ~]# echo $Product $Version
Python 2.7.13
[root@localhost ~]#

3. The assignment symbol

Double quotes: Allow other reference variable value by $ symbol;

[root@localhost ~]# echo "$Product"
java
[root@localhost ~]#

Single quotes: Do not quote the value of other variables, $ treated as an ordinary character;

[root@localhost ~]# echo '$Product'
$Product
[root@localhost ~]#

Anti apostrophe: Command Alternatively, after extraction of the output command.

[root@localhost ~]# ls -lh `which vim`
-rwxr-xr-x. 1 root root 2.2M 8月   2 2017 /usr/bin/vim
[root@localhost ~]#

Content input from the keyboard 4. Variable Assignment

Format: the Read [-p "message"] variable name

Script reads as follows:

[root@localhost ~]# vim script.sh
#!/bin/bash
read -p "请输入一个整数:" num
echo "您输入的整数是:$num"

Execution results are as follows:

[root@localhost ~]# chmod +x script.sh 
[root@localhost ~]# ./script.sh 
请输入一个整数:88
您输入的整数是:88
[root@localhost ~]#

5. Set a variable range of action

export command to a variable to a global variable, or else we bash variables in the current environment, in other bash environment becomes ineffective.

Formats: Export variable names
are two formats: Export variable name = variable value

[root@localhost ~]# echo $Product                      //输出变量
java
[root@localhost ~]# bash                                      //更换bash环境
[root@localhost ~]# echo $Product                     //输出变量
                                                                                 //已经找不到这个变量,因为这个bash环境没有该变量
[root@localhost ~]# exit                                       //退出,回到之前的bash环境
exit
[root@localhost ~]# export Product                   //将变量Product声明为全局变量
[root@localhost ~]# bash                                   //再次切换bash环境
[root@localhost ~]# echo $Product                 //输出变量
java                                                                      //输出成功
[root@localhost ~]#

6. The arithmetic integer variables

Format: expr Variable 1 Variable 2 operator [operator variable 3] ...

Common operator

加法运算:+
减法运算:-
乘法运算:\*                          //Linux系统中 “ * ” 为通配符,所以要加 “ \ ” 符号进行转译
除法运算:/
求模(取余)运算:%

Simple arithmetic demonstrates:

[root@localhost opt]# expr 11 + 22
33
[root@localhost opt]# expr 11 - 22
-11
[root@localhost opt]# expr 6 / 2
3
[root@localhost opt]# expr 6 \* 2
12
[root@localhost opt]# expr 6 % 4
2
[root@localhost opt]#

Special Shell Variables

Environment Variables

Environment variables are created in advance by the system, it is used to set the user's working environment.

System environment variables configuration file: / etc / Profile

User environment variable configuration file: ~ / .bash_profile

Common environmental variables

PWD、PATH、USER、SHLLE、HOME

[root@localhost opt]# echo $PWD
/opt
[root@localhost opt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost opt]# echo $USER
root
[root@localhost opt]# echo $HOME
/root
[root@localhost opt]# echo $SHELL
/bin/bash
[root@localhost opt]#

Position variable

He expressed as $ n, n is a number between 1 to 9.

Shell Scripting specification and variables

Simple addition script is as follows:

[root@localhost opt]# vim site.sh
#!/bin/bash
#执行加法运算
echo "第一个位置变量的值:$1"
echo "第二个位置变量的值:$2"
sum=`expr $1 + $2`
echo "求和数为$sum"

For adding the position variables:

[root@localhost opt]# chmod +x site.sh 
[root@localhost opt]# ./site.sh 33 45
第一个位置变量的值:33
第二个位置变量的值:45
求和数为78
[root@localhost opt]# 

Predefined variables

$#:命令行中位置变量的个数
$*:所有位置变量的内容
$?:上一条命令执行后返回的状态,当返回状态值为0时表示执行正常,非0值表示执行异常或出错
$0:当前执行的进程/程序名

Addition to the above script changes as follows:

#!/bin/bash
#执行加法运算
echo "第一个位置变量的值:$1"
echo "第二个位置变量的值:$2"
sum=`expr $1 + $2`
echo "求和数为$sum"
echo "变量个数为:$#"
echo "所有变量内容:$*"
echo "当前执行的脚本名称:$0"

Execution results are as follows:

第一个位置变量的值:33
第二个位置变量的值:45
求和数为78
变量个数为:2
所有变量内容:33 45
当前执行的脚本名称:./site.sh
[root@localhost opt]#

"? Echo $" command is finished, to see whether normal (the result is 0, the normal):

[root@localhost opt]# echo $?
0
[root@localhost opt]#

Guess you like

Origin blog.51cto.com/14449541/2441075