-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_note
More file actions
140 lines (105 loc) · 5.75 KB
/
script_note
File metadata and controls
140 lines (105 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
1. linux中shell变量$#,$@,$0,(,[的含义解释:
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"( [ … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"(" "[" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。(是第1参数、[是第2参数…。 ]))])])])
2. [ -n $1 ]:即第一个参数不为null
3. unset 可以删除变量
4. 获取字符串长度
string="abcd"
echo ${#string} #输出 4
5. 提取子字符串
以下实例从字符串第 2 个字符开始截取 4 个字符:
string="runoob is a great site"
echo ${string:1:4} # 输出 unoo
6. cut命令(字符串操作重要command)
(cut所指定的列下标从1开始计算)
https://www.cnblogs.com/Spiro-K/p/6361646.html
7. expr match $string 正则表达式
此命令可以找到string变量从头开始和正则表达式匹配的字符串的长度,如果在正则表达式中有了小括号,则代表不打印长度,而是将和括号中正则表达式部分的匹配的字符串输出
8. 替换
%s/正则表达式/目标字符串/g
其中,%s代表全文替换,g代表
9. shell中正则表达式
http://blog.51cto.com/13466287/2065604
http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html(不同命令所支持的正则表达式格式)
10. 字符串替换
第三种模式:${var/pattern/pattern}表示将var字符串的第一个匹配的pattern替换为另一个pattern。。
第四种模式:${var//pattern/pattern}表示将var字符串中的所有能匹配的pattern替换为另一个pattern.
11. shell大小写敏感
12. shell中,双小括号可以写算术表达式,双中括号可以写字符串匹配之类的表达式
13. awk命令(脚本)详解
http://www.runoob.com/linux/linux-comm-awk.html
NF可以取awk每一行元素个数
awk取每行最大值示例
https://www.cnblogs.com/irockcode/p/7215461.html
awk有两个特殊参数,OFS和ORS, 可以设置输出字段分隔符和行分隔符
14. sed命令(脚本)详解
https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html
sed命令用来查找、替换、删除、插入每一行数据
15. grep, awk, sed三者连用,基本就能够处理所有字符串相关的问题了
16. tr命令
即translate命令,可以将标准输入中的每一个字符都翻译
tr -d 可以删除输入中的某个字符
17. &> 代表正确输出和错误输出都重定向到后面指定的文件
2> 代表错误的输出都重定向到后面指定的文件
18. 想要利用source执行一个文件之后得到里面所设置的变量,必须使用export 来设置变量
在子shell中以及脚本中,可以得到所有此shell已经设置好的变量的值(包括环境变量)
19. 通过更改shell的PS1环境变量,可以更改shell交互界面的提示符,特殊符号包括\t,代表时间,\d,代表日期,\n,换行,\s,shell的名字,\w,当前工作目录,\W 当前工作目录的basename,
\u,用户名,\h,主机名,\\#,command number,\!,history number
如:PS1="\h:\u:\w>"
提示符会变为:bai-MS-7B92:bai:~/study/shell>
20. 0代表stdin,1代表stdout,2代表stderr,&可以代表stdout加上stderr
/dev/stdin /dev/stdout /dev/stderr是对应的三个文件
使用0,1,2的时候,读取时不需要加符号,写入时需要加上&,即2>&1代表讲错误写到stdout
21. tee命令主要是用来在命令输出到控制台的同时,将输出还保存到某个文件中
示例:cat file.txt 2>&1 | tee output.txt
22. gparted 可以用图形界面的方式更改分区
23. fdisk重要命令使用方式
1) n代表"new",p代表"print",分别是创建一个新分区和打印所有分区情况的意思
2) 在fdisk创建新分区需要你决定最后一个扇区的时候,可以使用+nG来创建一个nGB大小的分区
3) 使用mkfs.ext4(或者mkfs -t ext4) [分区名] 命令可以在新分区上面添加ext4文件系统(mkswap可以创建一个新的交换分区)
4) 之后要做的是挂载文件系统。
5) 为了在之后的使用中此磁盘可以开机自动挂载,还需要修改/etc/fstab文件中的内容,修改方式见下一条
24. 在启动系统时,磁盘会根据/etc/fstab中的配置内容被挂载
关于fstab文件的讲解:https://www.cnblogs.com/qiyebao/p/4484047.html
25. dumpe2fs命令可以查看各种关于磁盘的信息。基本上-h选项常用,是查看某分区的超级块信息
26. ~/.bashrc同时是login shell和non login shell必须执行的脚本
27. script中set命令
There are a number of options that can be used for debugging purposes:
set -n (bash -n) just checks for syntax
set -x (bash -x) echos all commands after running them
set -v (bash -v) echos all commands before running them
set -u (bash -u) causes the shell to treat using unset variables as an error
set -e (bash -e) causes the script to exit immediately upon any non-zero exit status
28. file conditions(test)
-e file file exists?
-d file file is a directory?
-f file file is a regular file?
-s file file has non-zero size?
-g file file has sgid set?
-u file file has suid set?
-r file file is readable?
-w file file is writeable?
-x file file is executable?
在括号里面加入!既可以表达“否”的含义
29. 编写/etc/rc.local文件,并使用 systemctl enable rc-local.service 命令可以让系统在启动的时候自动以root权限执行一些命令
30. sort命令详解:
https://www.cnblogs.com/fulucky/p/8022718.html
31. source . ./ sh bash区别:
https://www.cnblogs.com/pcat/p/5467188.html