Shell scripts of the first saw - theoretical articles (1)
Shell script concept:
The command to be executed in order to save a file
As long as the file is given execute permission, it will be able to run (using the chmod command)
May be combined to accomplish a variety of control statements shell complex operations
Shell scripts scenarios:
Repetitive operations
Batch processing
Automated operation and maintenance
Condition Monitoring Services
Timing task execution
Shell Script action:
As a command interpreter
Interposed between the kernel and user, is responsible for interpreting the command line
Write basic script code
1, using a text editor vim;
2, each line of a Linux command, executed in the order written.
Example:
[root@localhost ~]#vim first.sh //创建空白的shell脚本
cd /boot/
pwd //显示路径
ls -lh vml* //显示所有vml开头的文件
Given the script executable permissions
Making your script executable property - using the chmod command
Example:
[root@localhost ~]#chmod +x first.sh
&
[root@localhost ~]#chmod 755 first.sh
Execute the script file
method one:
"./ script file path"
Note: do not change their position when the method execute the script file.
[root@localhost ~]#./ first.sh //必须拥有执行权限
Method Two:
"Sh script file path"
Note: do not change their position when the method execute the script file.
[root@localhost ~]#sh first.sh //没有执行权限也可执行
Method three:
"Source script file path"
Note: it will change their position based on the contents of the script when the method execution.
[root@localhost ~]#source first.sh //没有执行权限也可执行
Method four:
. "Script file path"
Note: it will change their position based on the contents of the script when the method execution.
[root@localhost ~]#. first.sh //没有执行权限也可执行
Script constitute
1, the script declaration (declaration specific operating environment - must have)
2, annotation information (the content of the script to explain, you can not add)
3, executable statement
The most important variable --Shell script
effect
Provide specific parameters for the flexible management of Linux systems
1, variable names: use a fixed name, systematic preset or user-defined
2, variable values: can be set varies depending on the user, the system environment
Types of
Variables can be divided into four types:
Custom variable: There are user defined, modify and use
Environment variables: maintained by the system for setting the work environment
Location variables: to pass parameters to the script from the command line
Predefined variables: the Bash built into a class variable, can not be directly modified
Custom Variables
Variable names begin with a letter or an underscore, case-sensitive, it is recommended all uppercase.
Variable name = variable value
View a variable value
echo $ variable name
For the custom variable assignment can use quotation marks:
Double quotes: allows you to reference other variables by using the $ symbol
Single quotes: the $ symbol as ordinary prohibit references to other variables
Anti apostrophe ( `): command substitution, the result of command execution is extracted - the command for the intermediate trans apostrophe
It can be used when assigning read命令
keyboard input as the variable content
read [-p "message"] // the variable name entered manually entered values or strings, as a variable
Use custom variables integer arithmetic
format:
variable variable operator expr 1 2 [3 operator variable] ...
Operators used:
Adder: +
Subtraction: -
Multiplication: \
(\ as an escape character, only a number on behalf of a wildcard)Division: /
Modulo arithmetic:%
Environment Variables
Created by the system in advance, to set the user's working environment
Configuration file: / etc / Profile (global configuration), ~ / .bash_profile (specific account)
Common Environment Variables
PWD、PATH
USER、SHELL、HOME
Position variable
Predefined variables
$ #: The number of command line variable position
* $ : ** The contents of all position variables
$ ?: a state after the execution of the command, displaying a representation of true 0; display 1 indicates false
$ 0: currently executing process / program name