1. The idea before writing a script
Consider
Unified Script Management
Permissions: User execution permissions
Empty the error file
Error hints
Scripting Versatility
2.2 Unified Script Directory
1 mkdir /server/scripts2 cd/server/scripts
2.3 Creating a script with Vim
File name specification, name to make sense, end with. Sh End
Vim oldboyedu.sh
Canonical shell script, specifying interpreter
1 1. Put in the Unified directory2 2. Script ends with. SH3 3. Start specifying the script interpreter4 4. Information such as copyright, can be configured ~/. vimrc files are automatically added5 5. Scripts do not use Chinese comments, try to comment in English6 6. Code writing good Habits7A. The paired content is written at once to prevent omission, [],"' ""wait8 B. [] There are spaces at both ends first enter [] input backspace two spaces out of the grid9 C. Process Control statements finish once, then add contentTen d. Make code easy to read by indenting OneE. The quotation marks in the script are in the English state quotation marks, the other characters are also English state, good habit can let us avoid a lot of unnecessary trouble, improve work efficiency
2.4 Ways to execute Scripts
1. sh/server/scritps/test.sh
2. chmod +x/server/scripts/test.sh &&/server/scripts/test.sh
3. source/server/scripts/test.sh
4.. /server/scripts/test.sh. Equivalent to Source
Conclusion: The SH command executes the script in the current shell (process a), creates a new shell (process B), executes the script content, and finally passes the final result of the script to the current shell (process a) source, executing the script directly in the current shell (process a). So the variables of the script take effect in the current shell (process a)
Usage Scenarios
#引入文件时
Source/etc/profile
Source/etc/init.d/functions
Other sh/server/scripts/test.sh
2.5 Global Variables
1. Global variables, available everywhere. The current shell and all child shell windows are in effect. In the newly opened Shell window after the aftereffect, you need to write to the file.
2. Add environment variables via export
1 #此处为临时添加 2 #方法1 3 export hello="Hello World"4#方法25 hello =" HelloWorld"6 Export Hello
View Code
3. Everywhere in effect write config file echo ' export Hello=hello world ' >>/etc/profile
4. unset Delete a variable
2.8 Environment Variable configuration file
Global environment variable settings file/ETC/PROFILE/ETC/BASHRC
Local environment variable settings file ~/.bash_profile ~/.BASHRC
Boot order of environment variables after boot
#将环境变量时间放到文件结尾
1[Email protected] ~]#Env|Egrep "[Bb]ash|profile"|awk-F"=" 'Nr>1{print $1,$2}'|Sort-nk2|column2-T3Profile15121880414Bashrc15121880425Rootbashrc15121880436Rootbash_profile1512188044
View Code
#将环境变量时间放到文件开头,
1[Email protected] ~]#Env|Egrep "[Bb]ash|profile"|awk-F"=" 'Nr>1{print $1,$2}'|Sort-nk2|column-T2Profile15121938083Rootbash_profile15121938094Rootbashrc15121938105Bashrc1512193811
View Code
#利用文件属性查看加载过程
1 for file inch Do 2 stat ${fileawk'nr==5{print $2$3}; Nr==1{print }'3 done
View Code
2.9 Old Shell Vulnerability detection
The following code runs in the system and, if there are two lines or more, there is a shell vulnerability that requires an upgrade of the shell
1 env x='() {:;}; echo Be careful'"echo This is a test"
View Code
Shell (i)