Shell script-basic editing specifications and variables
Shell script overview
Used to interpret commands between the system kernel and users. "翻译官"
Save the commands to be executed to a text file in order.
Give the file executable permissions [默认权限为644所以给予权限"+x"]
. Various Shell control statements can be combined to complete more complex operations.
Shell script application scenarios
►Repetitive operation
►Interactive tasks
►Batch transaction processing
►Service operation status monitoring
Regular inspections | Perform optimization | Save logs
►Timed task execution
Shell operating environment
Logged-in user Shell
to automatically load when a Shell
program
bash
is the Linux
system used by default Shell
program
bash
files are located/bin/bash
cat /etc/shells
The composition of a shell script
First you need to declare an interpreter on the first line
Shell:#!/bin/bash
Indicates that the following code statements are executed through the /bin/bash program to interpret and execute
Python: #!/usr/bin/python
Expect:#!/usr/bin/expect
Next can be a comment# or a command statement
!#/bin/bash //解释器
#There is nothing here //注释
echo ”阿巴阿巴“ &> /dev/null //丢入无尽黑洞
Shell script execution
①Pass ./Execute script
echo "echo awsl" > start.sh
We can’t execute it at this time, because we don’t have enough permissions
chmod +x start.sh
By chmod
lifting its executable permissions
At this point it shined and that can be executed at any time
./start.sh
Absolute/relative path: bash environment execution will not change the operating environment in the file
②Execute the script through sh [no permission required]
At this point we subtract its permissions
chmod -x start.sh
sh start.sh
③Execute script through source [no permission required]
source start.sh
sh
And source
although no need to execute permissions can be performed, but it will inherit the current Shell
environment as an execution environment
Redirection and pipeline operations
①Switching hardware equipment
Types of | Default device | Device file | Device description number |
---|---|---|---|
Standard input | keyboard | /dev/stdin | 0 |
Standard output | monitor | /dev/stdout | 1 |
Guarantee error | monitor | /dev/stderr | 2 |
②Redirection operation
Types of | Operator | use |
---|---|---|
Redirect input | < | Read data from the specified file, non-keyboard input |
Redirect output | > | The output result will overwrite the original content |
Redirect output | >> | The output is appended to the end of the file |
Standard error output | 2> | Save the error message and overwrite the original content |
Standard error output | 2>> | Append error information to the specified file |
Mixed output | &> | Save the standard output and error content to the same file |
Mixed output | 2>&1 | Redirect standard error output to standard output |
For example:
echo 123456 > passwd;passwd fox --stdin < passwd;
When SeLinux is turned on, this operation will be rejected
③Pipeline operator
Use the output result of the command on the left as the input object of the command on the right. Multiple pipe characters can be used in the same line of command
命令 | 命令
④Control of awk column
Awk -F
Control bar, the control paragraph F:
indicates segmentation symbols, colon, if not written, and a space default identification tab as single quote as the content segmentation, print
printing
- NR indicates which row is the row currently being processed
- FNR indicates that the currently processed line is the first few lines of the currently processed file
- NF indicates how many columns of data the current row has
The role of shell variables
Used to store specific parameter values that the system and users need to use
►Custom variables: defined, modified and used by the user
►Environmental variables: use fixed names. Preset by the system or defined by the user
►Position variable: changes according to the changes of the user setting system
►Predefined variables: Bash
A type of variables built in. Can not be modified
①Custom variables
Define a new variable
usually start with a letter or underscore, case sensitive, all uppercase is recommended
变量名=变量值
View the value of a variable echo $变量名
- Double quotes: Allow to quote other variable values through the $ symbol
- Single quote: It is forbidden to quote other variable values, and $ is regarded as a normal character
- Backtick: command replacement, extract the output result after the command is executed
②Enter the content from the keyboard through read to assign a value to the variable
Use -p
to add a hint
read -p "是否打响指" input
echo ${input}
Variable scope
By default, the new variables defined only in the current Shell
active context, referred to as local variables
by internal command export
variable specified export global variable, so that bash
when the switch is still effective environment variables
export 变量名
export 变量名=变量值
Operation of integer variables
Operators: + addition,-subtraction, * multiplication, / division,% remainder
common algorithm expressions
expr 变量1 运算符 变量2
+ - \*[乘法运算] /除法运算 %求模(取余)
num=$(expr 2 \* 2)
num=`expr 2\*2`
num=$((2*2))
num=$[2*2]
let num=2*2
i++ 相当于 i=$[i+1]
i-- 相当于 i=$[i-1]
i+=2 相当于 i=$[i+2]
Special shell variables
①Environmental variables
Created by the system in advance, to set the user's working environment
profiles: /etc/profile
, ~/.bash+pro
use the env
command to view the current environment variables in the environment
variable USER
indicates the user names, HOME
representing the user's home directory, LANG
representation languages and character sets
②Read-only variables
Used for variable values not to be modified
product=benet
readonly product
③Position variable
When executing a command line operation, the first field represents the name of the command or script program, and the remaining string parameters are assigned to the positional variables in order from left to right.
vim start.sh
#!/bin/bash
echo $1
echo $2
echo $1 + $2
④Predetermined variables
$* $@ 表示命令或脚本要处理的参数
$* 把所有参数看成以空格分隔的一个字符串整体返回
11 22 33 44 为一个整体
$@ 加上双引号后会分割都是整体
11 22 33 44 他们每个都是整体
$# 表示命令或脚本处理参数的个数
$? 表示前一条命令或脚本执行后的返回状态码 [0为true非0为false]
time=backup-`date +%Y%m%d`.tgz
tar zcf $time $* &> /dev/null
echo "已执行 $0 个脚本,"
echo "共完成 $# 个对象的备份"
echo "具体内容包括: $*"
Calculation tool
①Use bc operation
bash itself does not support floating point operations
echo "1+2" | bc
scale=整数 #保留小数个数
echo "scale=5;1.11111+2.22222" | bc
②Use awk operation
Awk displays five decimal places and six significant digits by default
num=$(awk 'BEGIN {print 5/3}')
echo $num