Article Directory
[Station B-Chao Ge said shell notes]
The content is incomplete, only what I think is important is recorded! ! !
Refer to Acridine as appropriate
common basis
shell scripting – escaping and quoting
Shell backticks, ( ) and ( ) andThe difference between ( ) and {}
1. Special variables
$0 #当前文件名
$? #上一条命令的执行结果
# 关于传参
$# #参数个数
$1,$2,...$n #取输入参数值
$! #获取上一次后台执行的程序的PID
$$ #获取当前脚本的PID
$_ #获取上一条命令的最后一个参数
Interview: How to let the program execute in the background?
ans:
# 以某shell脚本为例
nohup bash ***.sh & 1> /dev/null
Two, shell substring
2.1 Basic built-in commands
echo #不可以自动识别特殊字符,需要加参数
printf #可以自动识别特殊字符,但是默认不换行
eval #执行多个命令;
exec #不创建子进程,执行后续命令,且执行完毕后,自动exit
export #取环境变量
read #接收命令行输入
shift #参数左移
test #
For details, please refer to:
shell programming | shift command usage notes, author: Wen Jiasange
rookie tutorial | shell -test usage
The following is a detailed introduction of the above built-in commands, if you know it, you can skip it.
echo
-n #不换行输出
-e #解析字符串中的特殊字符
\n #换行
\r #回车
\t #制表符
\b #退格
eval
executes multiple commands
eval ls ; cd /home #会先执行 ls 命令;再切换至/home目录
exec
Do not create a child process, execute subsequent commands, and automatically exit after execution
exec date
2.2 Playing tricks of shell substring
#不一定常用,记录最常用的几个
${var} #返回变量var的值
${
#var} #返回变量var的长度,有多少个字符
${var:start} #返回变量start数值之后的字符
${var:start:length} #提取start之后的length限制的字符
${var#word} #从变量开头,删除最短匹配的word字串
${var##word} #从变量开头删除最长匹配的word字串
${var%word} #从变量结尾删除最短的word
${var%%word} #从变量结尾删除最长匹配的word
${var/pattern/string} #用string代替第一个匹配的pattern
${var//pattern/string} #用string代替所有的pattern
Explain a few commonly used
# 0,1,2,3...
name="helloworld"
${
#name} # 返回值为10
${name:4} # 返回 world
${name#hello} #返回 world
${name%world} #返回 hello
${name/hello/fuck} #返回 fuckworld
2.3 Calculation of variable length
${#name}
expr
wc -L
awk
for
round robin rule
# 多行
for num in {
1..100}
do
echo ${name}
done
# 一行
for num in {
1..100};do echo ${name};done
seq
Order
seq 3
# 1
# 2
# 3
seq -s ":" 3 # 1:2:3
for n in {
1..3}
do
str=`seq -s ":" 10`
echo $str
done
time
Command
to be continued