上传文件至 'Shell-MD'
This commit is contained in:
parent
23952f59a4
commit
9825a094c6
|
@ -0,0 +1,517 @@
|
|||
<h1><center>shell三剑客</h1></center>
|
||||
|
||||
作者:行癫(盗版必究)
|
||||
|
||||
------
|
||||
|
||||
## 一:非交互式编辑器Sed
|
||||
|
||||
#### 1.sed介绍
|
||||
|
||||
![image-20230408155357663](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230408155357663.png)
|
||||
|
||||
sed 是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出;Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等
|
||||
|
||||
#### 2.语法格式
|
||||
|
||||
```shell
|
||||
sed [options] 'command' in_file[s]
|
||||
```
|
||||
|
||||
options部分
|
||||
|
||||
```
|
||||
-n 静默输出(不打印默认输出) sed -n '1p' a.txt 想显示第几行就显示第几行
|
||||
-e 给予sed多个命令的时候需要-e选项
|
||||
#sed -e 's/root/haha/g' -e 's/bash/wwwww/g' passwd > passwd.bak
|
||||
如果不用-e选项也可以用分号“;”把多个命令隔开。
|
||||
#sed 's/haha/ro/g ; s/wwwww/kkkk/g' passwd | less 这个是-e的结果
|
||||
-i -i后面没有扩展名的话直接修改文件,如果有扩展名备份源文件,产生以扩展名结尾的新文件
|
||||
#sed -iback1 -e 's/root/rottt/g' -e 's/bash/wwwww/g' passwd //选项-i后面没有空格
|
||||
[root@localhost 桌面]# ls
|
||||
manifest.txt passwdback1
|
||||
-f 当有多个要编辑的项目时,可以将编辑命令放进一个脚本里,再使用sed搭配-f选项
|
||||
[root@localhost 桌面]# cat s.sed
|
||||
s/bin/a/g
|
||||
s/ftp/b/g
|
||||
s/mail/c/g
|
||||
[root@localhost 桌面]# sed -f s.sed passwd | less
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
基本正则 sed
|
||||
|
||||
扩展正则 sed -r 无论是扩展正则还是基本正则,全部加r参数
|
||||
|
||||
command部分
|
||||
|
||||
```shell
|
||||
p 打印行 1p 输出再打印一遍第一行 1~2 打印奇数 0~2打印偶数
|
||||
d 删除文本
|
||||
#sed '1 d' passwd
|
||||
#sed '$ d' passwd
|
||||
#sed '1,3 d' passwd
|
||||
#sed '1,/^dian/ d' passwd
|
||||
a 追加文本(后)
|
||||
#sed '2 a nihao' passwd
|
||||
#sed '/^dian/ a nihao' passwd
|
||||
i 前插
|
||||
# sed -i '1 i nihao' passwd
|
||||
c 替换 sed '/zhong/c abc' 将zhong这一行替换成abc
|
||||
#sed -i '1 c no' passwd
|
||||
```
|
||||
|
||||
#### 3.sed案例
|
||||
|
||||
```shell
|
||||
1. sed可以从stdin中读取内容
|
||||
$ cat filename | sed 's/pattern/replace_string/'
|
||||
|
||||
2. 选项-i会使得sed用修改后的数据替换原文件
|
||||
$ sed -i 's/pattern/replace_string/' filename
|
||||
|
||||
3. g标记可以使sed执行全局替换
|
||||
$ sed 's/pattern/replace_string/g' filename
|
||||
|
||||
4. g标记可以使sed匹配第N次以后的字符被替换
|
||||
$ echo "thisthisthisthis" | sed 's/this/THIS/2g'
|
||||
|
||||
5. sed中的分隔符可以替换成别的字符, 因为s标识会认为后面的字符为分隔符
|
||||
$ sed 's:text:replace_text:'
|
||||
$ sed 's|text|replace_text|'
|
||||
|
||||
6. sed可以利用指令来删除文件中的空行
|
||||
$ sed '/^$/d' filename
|
||||
|
||||
7. 替换指定的字符串或数字
|
||||
$ cat sed_data.txt
|
||||
11 abc 111 this 9 file contains 111 11 99 numbers 0000
|
||||
$ sed -i 's/\b[0-9]\{3\}\b/NUMBER/g' sed_data.txt
|
||||
$ cat sed_data.txt
|
||||
11 abc NUMBER this 9 file contains NUMBER 11 99 numbers 0000
|
||||
|
||||
8. 由于在使用-i参数时比较危险, 所以我们在使用i参数时在后面加上.bak就会产生一个备份的文件,以防后悔
|
||||
$ sed -i.bak 's/pattern/replace_string/' filename
|
||||
```
|
||||
|
||||
## 二:文本处理awk
|
||||
|
||||
#### 1.awk介绍
|
||||
|
||||
awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk的处理文本和数据的方式是这样的,它逐行扫描文件,从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作。如果没有指定处理动作,则把匹配的行显示到标准输出(屏幕),如果没有指定模式,则所有被操作所指定的行都被处理。awk分别代表其作者姓氏的第一个字母。因为它的作者是三个人,分别是Alfred Aho、Brian Kernighan、Peter Weinberger。gawk是awk的GNU版本,它提供了Bell实验室和GNU的一些扩展
|
||||
|
||||
#### 2.语法格式
|
||||
|
||||
```shell
|
||||
awk [options] 'commands' filenames
|
||||
```
|
||||
|
||||
options部分
|
||||
|
||||
```shell
|
||||
POSIX options: GNU long options: (standard)
|
||||
-f progfile --file=progfile 指定awk脚本文件
|
||||
-F fs --field-separator=fs 定义输入字段分隔符,默认的分隔符是空格或制表符(tab)
|
||||
-v var=val --assign=var=val 定义变量并赋值
|
||||
```
|
||||
|
||||
command部分
|
||||
|
||||
```shell
|
||||
awk BEGIN{} {} END{} 文件
|
||||
BEGIN{} {} END{}
|
||||
行处理前 行处理 行处理后
|
||||
```
|
||||
|
||||
BEGIN{} 所有文本内容读入之前要执行的命令 可以不需要后面跟文件,因为他是在读入文件之前的操作
|
||||
|
||||
{} 主输入循环 读入一行命令执行一次循环
|
||||
|
||||
END{} 所有文本都读入完成之后执行的命令 必须要读入文件,因为他是在读入文件之后的操作
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
# awk 'BEGIN{print 1/2} {print "ok"} END{print "-----------"}' /etc/hosts
|
||||
0.5
|
||||
ok
|
||||
ok
|
||||
ok
|
||||
-----------
|
||||
|
||||
BEGIN{} 通常用于定义一些变量,例如BEGIN{FS=":";OFS="---"}
|
||||
```
|
||||
|
||||
常用案例:
|
||||
|
||||
```shell
|
||||
awk 'pattern' filename 示例:awk -F: '/root/' /etc/passwd
|
||||
awk '{action}' filename 示例:awk -F: '{print $1}' /etc/passwd
|
||||
awk 'pattern {action}' filename 示例:awk -F: '/root/{print $1,$3}' /etc/passwd
|
||||
示例:awk 'BEGIN{FS=":"} /root/{print $1,$3}' /etc/passwd
|
||||
command |awk 'pattern {action}' 示例:df -P| grep '/' |awk '$4 > 25000 {print $4}'
|
||||
```
|
||||
|
||||
#### 3.工作原理
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '{print $1,$3}' /etc/passwd
|
||||
(1)awk使用一行作为输入,并将这一行赋给内部变量$0,每一行也可称为一个记录,以换行符结束
|
||||
|
||||
root : x : 0 : 0 : root : /root : /bin/bash
|
||||
1 2 3 4 5 6 7
|
||||
(2)然后,行被:(默认为空格或制表符)分解成字段(或域),每个字段存储在已编号的变量中,从$1开始,最多达100个字段
|
||||
|
||||
(3)awk如何知道用空格来分隔字段的呢? 因为有一个内部变量FS来确定字段分隔符。初始时,FS赋为空格
|
||||
|
||||
(4)awk打印字段时,将以设置的方法使用print函数打印,awk在打印的字段间加上空格,因为$1,$3之间有一个逗号。逗号比较特殊,它映射为另一个内部变量,称为输出字段分隔符OFS,OFS默认为空格
|
||||
|
||||
(5)awk输出之后,将从文件中获取另一行,并将其存储在$0中,覆盖原来的内容,然后将新的字符串分隔成字段并进行处理。该过程将持续到所有行处理完毕
|
||||
```
|
||||
|
||||
#### 4.内建变量
|
||||
|
||||
```shell
|
||||
$0: awk变量$0保存当前记录的内容
|
||||
[root@xingdiancloud ~]# awk -F: '{print $0}' /etc/passwd
|
||||
NR: The total number of input records seen so far.
|
||||
[root@xingdiancloud ~]# awk -F: '{print NR, $0}' /etc/passwd /etc/hosts
|
||||
FNR: The input record number in the current input file
|
||||
[root@xingdiancloud ~]# awk -F: '{print FNR, $0}' /etc/passwd /etc/hosts
|
||||
NF: 保存记录的字段数,$1,$2...$100
|
||||
[root@xingdiancloud ~]# awk -F: '{print $0,NF}' /etc/passwd
|
||||
FS: 输入字段分隔符,默认空格
|
||||
[root@xingdiancloud ~]# awk -F: '/alice/{print $1, $3}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F'[ :\t]' '{print $1,$2,$3}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"} {print $1,$3}' /etc/passwd
|
||||
OFS: 输出字段分隔符
|
||||
[root@xingdiancloud ~]# awk -F: '/alice/{print $1,$2,$3,$4}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"; OFS="+++"} /^root/{print $1,$2,$3,$4}' passwd
|
||||
RS The input record separator, by default a newline. 默认是回车
|
||||
[root@xingdiancloud ~]# awk -F: 'BEGIN{RS=" "} {print $0}' a.txt
|
||||
ORS The output record separator, by default a newline.
|
||||
[root@xingdiancloud ~]# awk -F: 'BEGIN{ORS=""} {print $0}' passwd
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
字段分隔符: FS OFS 默认空格或制表符
|
||||
|
||||
记录分隔符: RS ORS 默认换行符
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{ORS=" "} {print $0}' /etc/passwd
|
||||
#将文件每一行合并为一行
|
||||
ORS默认输出一条记录应该回车,加了一个空格
|
||||
|
||||
[root@xingdiancloud ~]# head -1 /etc/passwd > passwd1
|
||||
[root@xingdiancloud ~]# cat passwd1
|
||||
root:x:0:0:root:/root:/bin/bash
|
||||
[root@xingdiancloud ~]#
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{RS=":"} {print $0}' passwd1
|
||||
root
|
||||
x
|
||||
0
|
||||
0
|
||||
root
|
||||
/root
|
||||
/bin/bash
|
||||
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{RS=":"} {print $0}' passwd1 |grep -v '^$' > passwd2
|
||||
```
|
||||
|
||||
#### 5.格式化输出
|
||||
|
||||
print函数
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# date |awk '{print "Month: " $2 "\nYear: " $NF}'
|
||||
[root@xingdiancloud ~]# awk -F: '{print "username is: " $1 "\t uid is: " $3}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{print "\tusername and uid: " $1,$3 "!"}' /etc/passwd
|
||||
```
|
||||
|
||||
printf函数
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{printf "|%-15s| %-10s| %-15s|\n", $1,$2,$3}' /etc/passwd
|
||||
|
||||
%s 字符类型
|
||||
%d 数值类型
|
||||
%f 浮点类型
|
||||
占15字符
|
||||
- 表示左对齐,默认是右对齐
|
||||
printf默认不会在行尾自动换行,加\n
|
||||
```
|
||||
|
||||
#### 6.awk模式和动作
|
||||
|
||||
任何awk语句都由模式和动作组成。模式部分决定动作语句何时触发及触发事件。处理即对数据进行的操作。如果省略模式部分,动作将时刻保持执行状态。模式可以是任何条件语句或复合语句或正则表达式。模式包括两个特殊字段 BEGIN和END。使用BEGIN语句设置计数和打印头。BEGIN语句使用在任何文本浏览动作之前,之后文本浏览动作依据输入文本开始执行。END语句用来在awk完成文本浏览动作后打印输出文本总数和结尾状态
|
||||
|
||||
##### 模式
|
||||
|
||||
正则表达式
|
||||
|
||||
```shell
|
||||
匹配记录(整行):~匹配
|
||||
[root@xingdiancloud ~]# awk '/^alice/' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk '$0 ~ /^alice/' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk '!/alice/' passwd
|
||||
[root@xingdiancloud ~]# awk '$0 !~ /^alice/' /etc/passwd
|
||||
|
||||
匹配字段:匹配操作符(~ !~)
|
||||
[root@xingdiancloud ~]# awk -F: '$1 ~ /^alice/' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$NF !~ /bash$/' /etc/passw
|
||||
```
|
||||
|
||||
比较表达式
|
||||
|
||||
比较表达式采用对文本进行比较,只有当条件为真,才执行指定的动作。比较表达式使用关系运算符,用于比较数字与字符串
|
||||
|
||||
```shell
|
||||
运算符 含义 示例
|
||||
< 小于 x<y
|
||||
<= 小于或等于 x<=y
|
||||
== 等于 x==y
|
||||
!= 不等于 x!=y
|
||||
>= 大于等于 x>=y
|
||||
> 大于 x>y
|
||||
```
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '$3 == 0' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$3 < 10' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$NF == "/bin/bash"' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$1 == "alice"' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$1 ~ /alic/ ' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$1 !~ /alic/ ' /etc/passwd
|
||||
[root@xingdiancloud ~]# df -P | grep '/' |awk '$4 > 25000'
|
||||
```
|
||||
|
||||
条件表达式
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '$3>300 {print $0}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{ if($3>300) {print $0} }' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{ if($3>300) {print $3} else{print $1} }' /etc/passwd
|
||||
```
|
||||
|
||||
算术运算
|
||||
|
||||
```shell
|
||||
+ - * / %(模) ^(幂2^3)
|
||||
|
||||
[root@xingdiancloud ~]# awk -F: '$3 * 10 > 500' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{ if($3*10>500){print $0} }' /etc/passwd
|
||||
```
|
||||
|
||||
逻辑操作符和复合模式
|
||||
|
||||
```shell
|
||||
&& 逻辑与 a&&b
|
||||
|| 逻辑或 a||b
|
||||
! 逻辑非 !a 除了这个以外的
|
||||
|
||||
[root@xingdiancloud ~]# awk -F: '$1~/root/ && $3<=15' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '$1~/root/ || $3<=15' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '!($1~/root/ || $3<=15)' /etc/passwd
|
||||
```
|
||||
|
||||
##### 示例
|
||||
|
||||
```ini
|
||||
[root@xingdiancloud ~]# awk '/west/' datafile
|
||||
[root@xingdiancloud ~]# awk '/^north/' datafile
|
||||
[root@xingdiancloud ~]# awk '$3 ~ /^north/' datafile
|
||||
[root@xingdiancloud ~]# awk '/^(no|so)/' datafile
|
||||
[root@xingdiancloud ~]# awk '{print $3,$2}' datafile
|
||||
|
||||
[root@xingdiancloud ~]# awk '{print $3 $2}' datafile
|
||||
[root@xingdiancloud ~]# awk '{print $0}' datafile
|
||||
[root@xingdiancloud ~]# awk '/northeast/{print $3,$2}' datafile
|
||||
[root@xingdiancloud ~]# awk '/E/' datafile
|
||||
|
||||
[root@xingdiancloud ~]# awk '/^[ns]/{print $1}' datafile
|
||||
[root@xingdiancloud ~]# awk '$5 ~ /\.[7-9]+/' datafile
|
||||
[root@xingdiancloud ~]# awk '$2 !~ /E/{print $1,$2}' datafile
|
||||
[root@xingdiancloud ~]# awk '$3 ~ /^Joel/{print $3 " is a nice boy."}' datafile
|
||||
[root@xingdiancloud ~]# awk '$8 ~ /[0-9][0-9]$/{print $8}' datafile
|
||||
|
||||
[root@xingdiancloud ~]# awk '$4 ~ /Chin$/{print "The price is $" $8 "."}' datafile
|
||||
[root@xingdiancloud ~]# awk '/Tj/{print $0}' datafile
|
||||
[root@xingdiancloud ~]# awk '{print $1}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{print $1}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk '{print "Number of fields: "NF}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{print "Number of fields: "NF}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F"[ :]" '{print NF}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F"[ :]+" '{print NF}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk '$7 == 5' datafile
|
||||
[root@xingdiancloud ~]# awk '$2 == "CT" {print $1, $2}' datafile
|
||||
[root@xingdiancloud ~]# awk '$7 != 5' datafile
|
||||
|
||||
[root@xingdiancloud ~]# cat b.txt
|
||||
xingdian sheng:is a::good boy!
|
||||
[root@xingdiancloud ~]# awk '{print NF}' b.txt
|
||||
4
|
||||
[root@xingdiancloud ~]# awk -F: '{print NF}' b.txt
|
||||
4
|
||||
[root@xingdiancloud ~]# awk -F"[ :]" '{print NF}' b.txt
|
||||
7
|
||||
[root@xingdiancloud ~]# awk -F"[ :]+" '{print NF}' b.txt
|
||||
6
|
||||
|
||||
[root@xingdiancloud ~]# awk '$7 < 5 {print $4, $7}' datafile #{if($7<5){print $4,$7}}
|
||||
[root@xingdiancloud ~]# awk '$6 > 9 {print $1,$6}' datafile
|
||||
[root@xingdiancloud ~]# awk '$8 <= 17 {print $8}' datafile
|
||||
[root@xingdiancloud ~]# awk '$8 >= 17 {print $8}' datafile
|
||||
[root@xingdiancloud ~]# awk '$8 > 10 && $8 < 17' datafile
|
||||
|
||||
[root@xingdiancloud ~]# awk '$2 == "NW" || $1 ~ /south/ {print $1, $2}' datafile
|
||||
[root@xingdiancloud ~]# awk '!($8 == 13){print $8}' datafile #$8 != 13
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $5 + 10}' datafile
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $8 + 10}' datafile
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $5 + 10.56}' datafile
|
||||
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $8 - 10}' datafile
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $8 / 2 }' datafile
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $8 / 3 }' datafile
|
||||
[root@xingdiancloud ~]# awk '/southem/{print $8 * 2 }' datafile
|
||||
```
|
||||
|
||||
#### 7.脚本编程-条件判断
|
||||
|
||||
if语句
|
||||
|
||||
```shell
|
||||
{if(表达式){语句;语句;...}}
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0) {print $1 " is administrator."}}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3>0 && $3<1000){count++;}} END{print count}' /etc/passwd
|
||||
```
|
||||
|
||||
if...else语句
|
||||
|
||||
```shell
|
||||
{if(表达式){语句;语句;...}else{语句;语句;...}}
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0){print $1} else {print $7}}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0) {count++} else{i++} }' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0){count++} else{i++}} END{print "管理员个数: "count ; print "系统用户数: "i}' /etc/passwd
|
||||
```
|
||||
|
||||
if...else if...else语句
|
||||
|
||||
```shell
|
||||
{if(表达式1){语句;语句;...}else if(表达式2){语句;语句;...}else if(表达式3){语句;语句;...}else{语句;语句;...}
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0){i++} else if($3>999){k++} else{j++}} END{print i; print k; print j}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{if($3==0){i++} else if($3>999){k++} else{j++}} END{print "管理员个数: "i; print "普通用个数: "k; print "系统用户: "j}' /etc/passwd
|
||||
```
|
||||
|
||||
#### 8.脚本编程-循环
|
||||
|
||||
while:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{ i=1; while(i<=10){print i; i++} }'
|
||||
[root@xingdiancloud ~]# awk -F: '/^root/{i=1; while(i<=7){print $i; i++}}' passwd
|
||||
[root@xingdiancloud ~]# awk '{i=1; while(i<=NF){print $i; i++}}' /etc/hosts
|
||||
[root@xingdiancloud ~]# awk -F: '{i=1; while(i<=10) {print $0; i++}}' /etc/passwd //将每行打印10次
|
||||
[root@xingdiancloud ~]# cat b.txt
|
||||
111 222
|
||||
333 444 555
|
||||
666 777 888 999
|
||||
[root@xingdiancloud ~]# awk '{i=1; while(i<=NF){print $i; i++}}' b.txt //分别打印每行的每列
|
||||
111
|
||||
222
|
||||
333
|
||||
444
|
||||
555
|
||||
666
|
||||
777
|
||||
888
|
||||
999
|
||||
```
|
||||
|
||||
for:
|
||||
|
||||
```she
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{for(i=1;i<=5;i++){print i} }' //C风格for
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
[root@xingdiancloud ~]# awk -F: '{ for(i=1;i<=10;i++) {print $0} }' /etc/passwd //将每行打印10次
|
||||
[root@xingdiancloud ~]# awk -F: '{ for(i=1;i<=NF;i++) {print $i} }' passwd //分别打印每行的每列
|
||||
root
|
||||
x
|
||||
0
|
||||
0
|
||||
root
|
||||
/root
|
||||
/bin/bash
|
||||
bin
|
||||
x
|
||||
1
|
||||
1
|
||||
bin
|
||||
/bin
|
||||
/sbin/nologin
|
||||
```
|
||||
|
||||
#### 9.脚本编程-数组
|
||||
|
||||
案例一:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '{username[i++]=$1} END{print username[1]}' /etc/passwd
|
||||
bin
|
||||
[root@xingdiancloud ~]# awk -F: '{username[i++]=$1} END{print username[0]}' /etc/passwd
|
||||
root
|
||||
```
|
||||
|
||||
数组遍历:
|
||||
|
||||
按元素个数遍历
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '{username[x++]=$1} END{for(i=0;i<x;i++) print i,username[i]}' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{username[++x]=$1} END{for(i=1;i<=x;i++) print i,username[i]}' /etc/passwd
|
||||
```
|
||||
|
||||
按索引遍历
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk -F: '{username[x++]=$1} END{for(i in username) {print i,username[i]} }' /etc/passwd
|
||||
[root@xingdiancloud ~]# awk -F: '{username[++x]=$1} END{for(i in username) {print i,username[i]} }' /etc/passwd
|
||||
```
|
||||
|
||||
综合案例:
|
||||
|
||||
统计/etc/passwd中各种类型shell的数量(统计谁把谁当作索引)
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# awk 'BEGIN{FS=":"} {shells[$NF]++} END{for(i in shells){print i,shells[i]}}' /etc/passwd
|
||||
```
|
||||
|
||||
统计TCP不同状态的数量
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# netstat -ant |grep tcp |awk '{access_stat[$NF]++} END{for(i in access_stat ){print i,access_stat[i]}}'
|
||||
```
|
||||
|
||||
## 三:文本过滤grep
|
||||
|
||||
#### 1.grep介绍
|
||||
|
||||
grep: 在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行
|
||||
|
||||
egrep: 扩展的egrep,支持更多的正则表达式元字符
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,230 @@
|
|||
<h1><center>shell函数</center></h1>
|
||||
|
||||
作者:行癫(盗版必究)
|
||||
|
||||
------
|
||||
|
||||
## 一:函数
|
||||
|
||||
#### 1.函数介绍
|
||||
|
||||
Shell 函数的本质是一段可以重复使用的脚本代码,这段代码被提前编写好了,放在了指定的位置,使用时直接调取即可
|
||||
|
||||
#### 2.语法格式
|
||||
|
||||
```shell
|
||||
function name() {
|
||||
statements
|
||||
[return value]
|
||||
}
|
||||
```
|
||||
|
||||
function是 Shell 中的关键字,专门用来定义函数(可以省略)
|
||||
|
||||
name是函数名
|
||||
|
||||
statements是函数要执行的代码,也就是一组命令
|
||||
|
||||
return value表示函数的返回值,其中 return 是 Shell 关键字,专门用在函数中返回一个值(可以省略)
|
||||
|
||||
#### 3.函数定义
|
||||
|
||||
```shell
|
||||
myfunc(){
|
||||
echo "This is a new function"
|
||||
}
|
||||
```
|
||||
|
||||
#### 4.函数调用
|
||||
|
||||
直接用函数名字调用函数
|
||||
|
||||
#### 5.函数传参
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat hello.sh
|
||||
#!/bin/bash
|
||||
hello(){
|
||||
echo $1
|
||||
}
|
||||
hello xingdian
|
||||
[root@xingdiancloud ~]# bash hello.sh
|
||||
xingdian
|
||||
```
|
||||
|
||||
#### 6.函数变量
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat hello.sh
|
||||
#!/bin/bash
|
||||
i=0
|
||||
echo "$a"
|
||||
hello(){
|
||||
a=1
|
||||
local d=3
|
||||
echo "$i $a $b $c $d"
|
||||
}
|
||||
hello
|
||||
b=2
|
||||
echo "$a $d"
|
||||
[root@xingdiancloud ~]# bash hello.sh
|
||||
|
||||
0 1 3
|
||||
1
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
默认,函数里的变量会在函数外面生效
|
||||
|
||||
注意脚本中内容按上下文顺序执行
|
||||
|
||||
local定义的变量只在函数内生效
|
||||
|
||||
#### 7.调用函数
|
||||
|
||||
创建功能函数
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat hello.sh
|
||||
#!/bin/bash
|
||||
hello(){
|
||||
echo "This is one"
|
||||
}
|
||||
```
|
||||
|
||||
另一个脚本调用该脚本中函数
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat xingdian.sh
|
||||
#!/bin/bash
|
||||
source ./hello.sh
|
||||
hello
|
||||
[root@xingdiancloud ~]# bash xingdian.sh
|
||||
This is one
|
||||
```
|
||||
|
||||
#### 8.函数案例
|
||||
|
||||
```shell
|
||||
#!/bin/bash
|
||||
#v1.24.5.27.1
|
||||
#作者:行癫
|
||||
|
||||
list()
|
||||
{
|
||||
echo "+++++++++++++++++++++++++++++++++"
|
||||
echo "+++++++ 百宝箱 ++++++++"
|
||||
echo "+++++++++++++++++++++++++++++++++"
|
||||
echo "|||||||||||||||||||||||||||||||||"
|
||||
echo "================================="
|
||||
echo "= 1.yum仓库初始化 ="
|
||||
echo "= 2.上课笔记工具安装 ="
|
||||
echo "= 3.kvm虚拟机安装 ="
|
||||
echo "= 4.vmware虚拟机安装 ="
|
||||
echo "= 5.vs code安装 ="
|
||||
echo "= 6.google浏览器安装 ="
|
||||
echo "= 7.vnc-server的安装 ="
|
||||
echo "= 8.一键安装所有 ="
|
||||
echo "= 9.退出 ="
|
||||
echo "================================="
|
||||
}
|
||||
yum-install(){
|
||||
echo "====正在执行yum初始化操作请耐心等待!===="
|
||||
rm -rf /etc/yum.repos.d/*
|
||||
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null
|
||||
yum -y install wget > /dev/null
|
||||
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
|
||||
echo "====正在清空缓存,请耐心等待!===="
|
||||
yum clean all
|
||||
echo "====正在重新加载,请耐心等待!===="
|
||||
yum makecache
|
||||
echo "====successed===="
|
||||
}
|
||||
|
||||
cherrytree(){
|
||||
echo "====正在进行安装......====="
|
||||
yum -y install cherrytree > /dev/null
|
||||
if [ $? -eq 0 ];then
|
||||
echo "====successed===="
|
||||
else
|
||||
echo "==== failed ===="
|
||||
echo "====检查网络和yum仓库===="
|
||||
exit
|
||||
fi
|
||||
}
|
||||
|
||||
kvm-install(){
|
||||
echo "====正在安装kvm虚拟机===="
|
||||
yum -y install libvirt* virt-manager >> /dev/null && yum -y groupinstall 'Virtualization Host' >> /dev/null
|
||||
echo "==== successed ===="
|
||||
}
|
||||
|
||||
vmware-install(){
|
||||
echo "====请将vmware安装包放到当前目录下===="
|
||||
chmod +x
|
||||
echo "......."
|
||||
echo "..........."
|
||||
echo ".................100%"
|
||||
|
||||
}
|
||||
|
||||
data=`date | awk '{print $4}'`
|
||||
read -p "当前时间为${data},你是否要进行电脑初始化,继续请按回车(已记录你的初始时间):"
|
||||
|
||||
ping -c1 www.baidu.com 1> /dev/null
|
||||
|
||||
if [ $? -eq 0 ];then
|
||||
echo "网络状况良好,请继续~"
|
||||
else
|
||||
echo "网络状况不佳,检查网络~"
|
||||
exit
|
||||
fi
|
||||
|
||||
echo "xingdian" > user.txt
|
||||
echo "dianye" > password.txt
|
||||
|
||||
read -p "欢迎使用行癫工具箱,进行安装部署操作:"
|
||||
|
||||
read -p "请输入用户名:" name
|
||||
|
||||
username=`cat user.txt | awk '{print $1}'`
|
||||
passwd=`cat password.txt | awk '{print $1}'`
|
||||
|
||||
if [ "${name}" == "${username}" ];then
|
||||
read -p "请输入密码:" password
|
||||
if [ "${password}" == "${passwd}" ];then
|
||||
echo "登陆成功,进入工具箱"
|
||||
while :
|
||||
do
|
||||
list
|
||||
read -p "请选择你要使用的工具代码:" num
|
||||
case $num in
|
||||
1)
|
||||
yum-install
|
||||
sleep 3
|
||||
;;
|
||||
2)
|
||||
cherrytree
|
||||
sleep 3
|
||||
;;
|
||||
3)
|
||||
;;
|
||||
4)
|
||||
;;
|
||||
9)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
data2=`date | awk '{print $4}'`
|
||||
echo "结束时间为${data2};感谢您的使用!"
|
||||
else
|
||||
echo "用户名密码错误,请重新执行脚本!"
|
||||
exit
|
||||
fi
|
||||
else
|
||||
echo "用户名输入错误,请重新输入!"
|
||||
exit
|
||||
fis
|
||||
```
|
|
@ -0,0 +1,410 @@
|
|||
<h1><center>Shell变量</center></h1>
|
||||
|
||||
作者:行癫(盗版必究)
|
||||
|
||||
------
|
||||
|
||||
## 一:变量概述
|
||||
|
||||
#### 1.什么是变量
|
||||
|
||||
变量来源于数学,是计算机语言中能储存计算结果或能表示值的抽象概念
|
||||
|
||||
变量可以通过变量名访问,在指令式语言中,变量通常是可变的;在某些条件下也是不可变的
|
||||
|
||||
#### 2.变量的规则
|
||||
|
||||
命名只能使用英文字母,数字和下划线,首个字符不能以数字开头
|
||||
|
||||
中间不能有空格,可以使用下划线
|
||||
|
||||
不能使用标点符号
|
||||
|
||||
不能使用bash里的关键字
|
||||
|
||||
## 二:变量分类
|
||||
|
||||
#### 1.自定义变量
|
||||
|
||||
定义变量:变量名=变量值 例如:xingdian=123
|
||||
|
||||
引用变量:$变量名 或 ${变量名}
|
||||
|
||||
查看变量:echo $变量名
|
||||
|
||||
取消变量:unset 变量名
|
||||
|
||||
作用范围:仅在当前shell中有效
|
||||
|
||||
#### 2.环境变量
|
||||
|
||||
定义环境变量:
|
||||
|
||||
方法一 export back_dir2=/home/backup
|
||||
|
||||
方法二 export back_dir1 将自定义变量转换成环境变量
|
||||
|
||||
引用环境变量:$变量名 或 ${变量名}
|
||||
|
||||
查看环境变量:echo $变量名
|
||||
|
||||
取消环境变量:unset 变量名
|
||||
|
||||
变量作用范围:在当前shell和子shell有效
|
||||
|
||||
注意:
|
||||
|
||||
环境变量拥有可继承性:export之后就拥有继承性
|
||||
|
||||
永久生效:写到环境变量脚本,/etc/profile ~/.baserc ~/.bash_profile /etc/bashrc
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# vim /etc/profile
|
||||
JAVA_HOME=/usr/local/java
|
||||
PATH=$JAVA_HOME/bin:$PATH
|
||||
export JAVA_HOME PATH
|
||||
```
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# vim ~/.bash_profile (只显示部分)
|
||||
PATH=$PATH:$HOME/bin:/usr/local/mycat/bin
|
||||
```
|
||||
|
||||
/etc/profile
|
||||
|
||||
这是系统最主要的shell设置文件,也是用户登陆时系统最先检查的文件,有关重要的环境变量都定义在此,其中包括PATH,USER,LOGNAME,MAIL,HOSTNAME,HISTSIZE,INPUTRC等。而在文件的最后,它会检查并执行/etc/profile.d/*.sh的脚本
|
||||
|
||||
~/.bash_profile
|
||||
|
||||
这个文件是每位用户的bash环境设置文件,它存在与于用户的主目录中,当系统执行/etc/profile 后,就会接着读取此文件内的设置值。在此文件中会定义USERNAME,BASH_ENV和PATH等环境变量,但是此处PATH除了包含系统的$PATH变量外加入用户的“bin”目录路径
|
||||
|
||||
~/.bashrc
|
||||
|
||||
接下来系统会检查~.bashrc文件,这个文件和前两个文件(/etc/profile 和~.bash_profile)最大的不同是,每次执行bash时,~.bashrc都会被再次读取,也就是变量会再次地设置,而/etc/profile,~./bash_profile只有在登陆时才读取。就是因为要经常的读取,所以~/.bashrc文件只定义一些终端机设置以及shell提示符号等功能,而不是定义环境变量
|
||||
|
||||
~/.bash_login
|
||||
|
||||
如果~/.bash_profile文件不存在,则系统会转而读取~/.bash_login这个文件内容。这是用户的登陆文件,在每次用户登陆系统时,bash都会读此内容,所以通常都会将登陆后必须执行的命令放在这个文件中
|
||||
|
||||
.profile
|
||||
|
||||
如果~./bash_profile ~./bash_login两个文件都不存在,则会使用这个文件的设置内容,其实它的功能与~/.bash_profile相同
|
||||
|
||||
.bash_logout
|
||||
|
||||
如果想在注销shell前执行一些工作,都可以在此文件中设置
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# vi ~.bash_logout
|
||||
clear
|
||||
仅执行一个clear命令在你注销的时候
|
||||
```
|
||||
|
||||
~/.bash_history
|
||||
|
||||
这个文件会记录用户先前使用的历史命令
|
||||
|
||||
注意:
|
||||
|
||||
在/etc/profile.d建立独立的环境变量配置文件
|
||||
|
||||
常用环境变量:USER UID HOME HOSTNAME PWD PATH
|
||||
|
||||
PATH:这个变量存放的是所有命令所在的路径 修改:PATH=$PATH:+目录
|
||||
|
||||
#### 3.位置变量
|
||||
|
||||
$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud sh]# cat xingdian.sh
|
||||
#!/bin/bash
|
||||
echo "hello $1"
|
||||
[root@xingdiancloud sh]# bash xingdian.sh xingdian
|
||||
hello xingdian
|
||||
```
|
||||
|
||||
4.预定义变量
|
||||
|
||||
```shell
|
||||
$0 脚本名
|
||||
$* 所有的参数
|
||||
$@ 所有的参数
|
||||
$# 参数的个数
|
||||
$$ 当前进程的PID
|
||||
$! 上一个后台进程的PID
|
||||
$? 上一个命令的返回值 0表示成功
|
||||
```
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud sh]# cat test.sh
|
||||
#!/bin/bash
|
||||
echo "第2个位置参数是$2"
|
||||
echo "第1个位置参数是$1"
|
||||
echo "第4个位置参数是$4"
|
||||
|
||||
echo "所有参数是: $*"
|
||||
echo "所有参数是: $@"
|
||||
echo "参数的个数是: $#"
|
||||
echo "当前进程的PID是: $$"
|
||||
|
||||
echo '$1='$1
|
||||
echo '$2='$2
|
||||
echo '$3='$3
|
||||
echo '$*='$*
|
||||
echo '$@='$@
|
||||
echo '$#='$#
|
||||
echo '$$='$$
|
||||
```
|
||||
|
||||
## 三:变量赋值
|
||||
|
||||
#### 1.显示赋值
|
||||
|
||||
变量名=变量值
|
||||
|
||||
示例:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# ip1=192.168.1.251
|
||||
[root@xingdiancloud ~]# school="BeiJing 1000phone"
|
||||
[root@xingdiancloud ~]# today1=`date +%F`
|
||||
[root@xingdiancloud ~]# today2=$(date +%F)
|
||||
```
|
||||
|
||||
#### 2.键盘读入
|
||||
|
||||
```shell
|
||||
read 变量名
|
||||
read -p "提示信息: " 变量名
|
||||
read -t 5 -p "提示信息: " 变量名 -t 后面跟秒数,定义输入字符的等待时间
|
||||
read -n 2 变量名 -n 后跟一个数字,定义输入文本的长度,很实用。
|
||||
```
|
||||
|
||||
案例1:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# vim first.sh
|
||||
back_dir1=/var/backup
|
||||
read -p "请输入你的备份目录: " back_dir2
|
||||
echo $back_dir1
|
||||
echo $back_dir2
|
||||
[root@xingdiancloud ~]# sh first.sh
|
||||
```
|
||||
|
||||
案例2:
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# vim ping2.sh
|
||||
#!/bin/bash
|
||||
read -p "Input IP: " ip
|
||||
ping -c2 $ip &>/dev/null
|
||||
if [ $? = 0 ];then
|
||||
echo "host $ip is ok"
|
||||
else
|
||||
echo "host $ip is fail"
|
||||
fi
|
||||
[root@xingdiancloud ~]# chmod a+x ping2.sh
|
||||
[root@xingdiancloud ~]# ./ping.sh
|
||||
```
|
||||
|
||||
注意:定义或引用变量时注意事项
|
||||
|
||||
" " 弱引用 可以实现变量和命令的替换
|
||||
|
||||
' ' 强引用 不完成变量替换
|
||||
|
||||
反引 命令替换 等价于 $() 反引号中的shell命令会被先执行
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# school=1000phone
|
||||
[root@xingdiancloud ~]# echo "${school} is good"
|
||||
1000phone is good
|
||||
[root@xingdiancloud ~]# echo '${school} is good'
|
||||
${school} is good
|
||||
[root@xingdiancloud ~]# touch `date +%F`_file1.txt
|
||||
[root@xingdiancloud ~]# touch $(date +%F)_file2.txt
|
||||
[root@xingdiancloud ~]# disk_free3="df -Ph |grep '/$' |awk '{print $4}'" 错误
|
||||
[root@xingdiancloud ~]# disk_free4=$(df -Ph |grep '/$' |awk '{print $4}')
|
||||
[root@xingdiancloud ~]# disk_free5=`df -Ph |grep '/$' |awk '{print $4}
|
||||
```
|
||||
|
||||
## 四:变量运算
|
||||
|
||||
#### 1.整数运算
|
||||
|
||||
方法一:expr
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# expr 1 + 2
|
||||
[root@xingdiancloud ~]# expr $num1 + $num2 + - \* / %
|
||||
```
|
||||
|
||||
方法二:$(())
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo $(($num1+$num2)) + - * / %
|
||||
[root@xingdiancloud ~]# echo $((num1+num2))
|
||||
[root@xingdiancloud ~]# echo $((5-3*2))
|
||||
[root@xingdiancloud ~]# echo $(((5-3)*2))
|
||||
[root@xingdiancloud ~]# echo $((2**3))
|
||||
[root@xingdiancloud ~]# sum=$((1+2)); echo $sum
|
||||
```
|
||||
|
||||
方法三:$[]
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo $[5+2] + - * / %
|
||||
[root@xingdiancloud ~]# echo $[5**2]
|
||||
```
|
||||
|
||||
方法四:let
|
||||
|
||||
```
|
||||
[root@xingdiancloud ~]# let sum=2+3; echo $sum
|
||||
[root@xingdiancloud ~]# let i++; echo $i
|
||||
```
|
||||
|
||||
#### 2.小数运算
|
||||
|
||||
使用bc做小数运算,scale指定小数点位数
|
||||
|
||||
加法运算(scale参数无效)
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo "5.999 + 5.001"|bc
|
||||
6.000
|
||||
[root@xingdiancloud ~]# echo "5.111+ 5.1114"|bc
|
||||
10.2224
|
||||
```
|
||||
|
||||
减法运算(scale参数无效)
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo "2.22 - 1.11"|bc
|
||||
1.11
|
||||
```
|
||||
|
||||
乘法运算
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo "5.12 * 5.6000"|bc
|
||||
28.6720
|
||||
```
|
||||
|
||||
注意:乘积小数点位数默认以乘数中小数点位数最多的为准(不指定scale参数)
|
||||
|
||||
除法运算
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo "scale=2;9.898 / 1.11"|bc
|
||||
8.91
|
||||
[root@xingdiancloud ~]# echo "9.898 / 1.11"|bc
|
||||
8
|
||||
```
|
||||
|
||||
## 五:扩展
|
||||
|
||||
#### 1.内容的删除
|
||||
|
||||
案例一
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# url=www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${#url} 获取变量值的长度
|
||||
15
|
||||
[root@xingdian ~]# echo ${url} 标准查看
|
||||
www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url#*.} 从前往后,最短匹配
|
||||
sina.com.cn
|
||||
[root@xingdian ~]# echo ${url##*.} 从前往后,最长匹配 贪婪匹配
|
||||
cn
|
||||
[root@xingdian ~]# url=www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url#a.}
|
||||
www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url#*sina.}
|
||||
com.cn
|
||||
```
|
||||
|
||||
案例二
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# url=www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url}
|
||||
www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url%.*} 从后往前,最短匹配
|
||||
www.sina.com
|
||||
[root@xingdian ~]# echo ${url%%.*} 从后往前,最长匹配 贪婪匹配
|
||||
www
|
||||
[root@xingdian ~]# echo $HOSTNAME
|
||||
xingdian.1000phone.com
|
||||
[root@xingdian ~]# echo ${HOSTNAME%%.*}
|
||||
xingdian
|
||||
```
|
||||
|
||||
#### 2.索引及切片
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# echo ${url:0:5}
|
||||
0:从头开始
|
||||
5:到第五个
|
||||
[root@xingdian ~]# echo ${url:5:5}
|
||||
[root@xingdian ~]# echo ${url:5}
|
||||
```
|
||||
|
||||
#### 3.变量内容替换
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# url=www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url/sina/baidu}
|
||||
www.baidu.com.cn
|
||||
[root@xingdian ~]# url=www.sina.com.cn
|
||||
[root@xingdian ~]# echo ${url/n/N}
|
||||
www.siNa.com.cn
|
||||
[root@xingdian ~]# echo ${url//n/N} 贪婪匹配
|
||||
www.siNa.com.cN
|
||||
```
|
||||
|
||||
#### 4.自增运算
|
||||
|
||||
对变量的值的影响
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# i=1
|
||||
[root@xingdian ~]# let i++
|
||||
[root@xingdian ~]# echo $i
|
||||
2
|
||||
[root@xingdian ~]# j=1
|
||||
[root@xingdian ~]# let ++j
|
||||
[root@xingdian ~]# echo $j
|
||||
2
|
||||
```
|
||||
|
||||
对表达式的值的影响
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# unset i
|
||||
[root@xingdian ~]# unset j
|
||||
[root@xingdian ~]#
|
||||
[root@xingdian ~]# i=1
|
||||
[root@xingdian ~]# j=1
|
||||
[root@xingdian ~]#
|
||||
[root@xingdian ~]# let x=i++ 先赋值,再运算
|
||||
[root@xingdian ~]# let y=++j 先运算,再赋值
|
||||
[root@xingdian ~]#
|
||||
[root@xingdian ~]# echo $i
|
||||
2
|
||||
[root@xingdian ~]# echo $j
|
||||
2
|
||||
[root@xingdian ~]#
|
||||
[root@xingdian ~]# echo $x
|
||||
1
|
||||
[root@xingdian ~]# echo $y
|
||||
2
|
||||
```
|
|
@ -0,0 +1,205 @@
|
|||
<h1><center>shell数组</center></h1>
|
||||
|
||||
作者:行癫(盗版必究)
|
||||
|
||||
------
|
||||
|
||||
## 一:数组
|
||||
|
||||
#### 1.数组介绍
|
||||
|
||||
是若干数据的集合,其中的每一份数据都称为元素
|
||||
|
||||
shell不限制数组的大小,理论上可以存放无限量的数据
|
||||
|
||||
#### 2.数组分类
|
||||
|
||||
普通数组
|
||||
|
||||
只能使用整数作为数组索引/下标(从0开始)
|
||||
|
||||
关联数组
|
||||
|
||||
可以使用字符串作为数组索引/下标
|
||||
|
||||
#### 3.定义数组
|
||||
|
||||
##### 普通数组
|
||||
|
||||
方法一: 一次赋一个值
|
||||
|
||||
```shell
|
||||
数组名[索引]=变量值
|
||||
[root@xingdiancloud ~]# array1[0]=pear
|
||||
[root@xingdiancloud ~]# array1[1]=apple
|
||||
[root@xingdiancloud ~]# array1[2]=orange
|
||||
[root@xingdiancloud ~]# array1[3]=peach
|
||||
```
|
||||
|
||||
方法二: 一次赋多个值
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# array2=(tom jack alice)
|
||||
[root@xingdiancloud ~]# array5=(tom jack alice "bash shell")
|
||||
[root@xingdiancloud ~]# colors=($red $blue $green $recolor)
|
||||
[root@xingdiancloud ~]# array5=(1 2 3 4 5 6 7 "linux shell" [20]=saltstack)
|
||||
[root@xingdiancloud ~]# array8=`cat /etc/passwd`
|
||||
```
|
||||
|
||||
##### 关联数组
|
||||
|
||||
申明该数组为关联数组
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# declare -A ass_array1
|
||||
[root@xingdiancloud ~]# declare -A ass_array2
|
||||
```
|
||||
|
||||
方法一: 一次赋一个值
|
||||
|
||||
```shell
|
||||
数组名[索引]=变量值
|
||||
[root@xingdiancloud ~]# ass_array1[index1]=pear
|
||||
[root@xingdiancloud ~]# ass_array1[index2]=apple
|
||||
[root@xingdiancloud ~]# ass_array1[index3]=orange
|
||||
[root@xingdiancloud ~]# ass_array1[index4]=peach
|
||||
```
|
||||
|
||||
方法二: 一次赋多个值
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]='bash shell')
|
||||
```
|
||||
|
||||
一步走
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# declare -A ass_array1='([index4]="peach" [index1]="pear" [index2]="apple" [index3]="orange" )'
|
||||
[root@xingdiancloud ~]# declare -A ass_array2='([index4]="bash shell" [index1]="tom" [index2]="jack" [index3]="alice" )'
|
||||
```
|
||||
|
||||
#### 4.访问数组元素
|
||||
|
||||
##### 普通数组
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo ${array1[0]} 访问数组中的第一个元数
|
||||
[root@xingdiancloud ~]# echo ${array1[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
|
||||
[root@xingdiancloud ~]# echo ${#array1[@]} 统计数组元数的个数
|
||||
[root@xingdiancloud ~]# echo ${!array2[@]} 获取数组元数的索引
|
||||
[root@xingdiancloud ~]# echo ${array1[@]:1} 从数组下标1开始
|
||||
[root@xingdiancloud ~]# echo ${array1[@]:1:2} 从数组下标1开始,访问两个元素
|
||||
```
|
||||
|
||||
##### 关联数组
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# echo ${ass_array2[index2]} 访问数组中的第二个元数
|
||||
[root@xingdiancloud ~]# echo ${ass_array2[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
|
||||
[root@xingdiancloud ~]# echo ${#ass_array2[@]} 获得数组元数的个数
|
||||
[root@xingdiancloud ~]# echo ${!ass_array2[@]} 获得数组元数的索引
|
||||
```
|
||||
|
||||
#### 5.数组遍历
|
||||
|
||||
方法一: 通过数组元数的个数进行遍历
|
||||
|
||||
方法二: 通过数组元数的索引进行遍历
|
||||
|
||||
案例一:利用元素进行遍历
|
||||
|
||||
```shell
|
||||
#!/bin/bash
|
||||
#定义数组
|
||||
array=(Mon Tue Wed Thur Fir Sat Sun)
|
||||
#数组遍历
|
||||
for day in ${array[*]}
|
||||
do
|
||||
echo $day
|
||||
done
|
||||
```
|
||||
|
||||
案例二:利用索引进行遍历
|
||||
|
||||
```
|
||||
[root@xingdiancloud ~]# cat hello.sh
|
||||
#!/bin/bash
|
||||
for line in `cat /etc/hosts`
|
||||
do
|
||||
hosts[++j]=$line
|
||||
done
|
||||
|
||||
for i in ${!hosts[@]}
|
||||
do
|
||||
echo "$i : ${hosts[i]}"
|
||||
done
|
||||
[root@xingdiancloud ~]# bash hello.sh
|
||||
1 : 127.0.0.1
|
||||
2 : localhost
|
||||
3 : localhost.localdomain
|
||||
4 : localhost4
|
||||
5 : localhost4.localdomain4
|
||||
6 : ::1
|
||||
7 : localhost
|
||||
8 : localhost.localdomain
|
||||
9 : localhost6
|
||||
10 : localhost6.localdomain6
|
||||
```
|
||||
|
||||
定义换行符
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat hello.sh
|
||||
#!/bin/bash
|
||||
IFS=$'\n'
|
||||
for line in `cat /etc/hosts`
|
||||
do
|
||||
hosts[++j]=$line
|
||||
done
|
||||
|
||||
for i in ${!hosts[@]}
|
||||
do
|
||||
echo "$i : ${hosts[i]}"
|
||||
done
|
||||
[root@xingdiancloud ~]# bash hello.sh
|
||||
1 : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
|
||||
2 : ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
|
||||
```
|
||||
|
||||
#### 6.项目案例
|
||||
|
||||
通过数组统计性别:把要统计的对象作为数组的索引
|
||||
|
||||
```shell
|
||||
[root@xingdiancloud ~]# cat sex.txt
|
||||
zhangsan f
|
||||
lisi m
|
||||
|
||||
[root@xingdiancloud ~]# cat sex.sh
|
||||
#!/usr/bin/bash
|
||||
declare -A sex
|
||||
while read line
|
||||
do
|
||||
type=`echo $line | awk '{print $2}'`
|
||||
let sex[$type]++
|
||||
done < sex.txt
|
||||
#m作为数组的索引
|
||||
|
||||
for i in ${!sex[@]}
|
||||
do
|
||||
echo "$i : ${sex[$i]}"
|
||||
done
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
<h1><center>shell正则</center></h1>
|
||||
|
||||
作者:行癫(盗版必究)
|
||||
|
||||
------
|
||||
|
||||
## 一:正则
|
||||
|
||||
#### 1.正则介绍
|
||||
|
||||
正则表达式(regular expression, RE)是一种字符模式,用于在查找过程中匹配指定的字符
|
||||
|
||||
在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/l[oO]ve/就是由正斜杠界定的正则表达式
|
||||
|
||||
它将匹配被查找的行中任何位置出现的相同模式。在正则表达式中,元字符是最重要的概念
|
||||
|
||||
重要的文本处理工具:vim、sed、awk、grep
|
||||
|
||||
重要的应用软件:mysql、oracle、php、python、Apache、Nginx ...
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
匹配数字: ^[0-9]+$ 123 456 +表示前面的内容出现多次
|
||||
匹配Mail: [a-z0-9_]+@[a-z0-9]+\.[a-z]+ xingdian131420@126.com
|
||||
匹配IP: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} {1,3}数字出现1-3次
|
||||
[root@xingdian ~]# egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /etc/sysconfig/network-scripts/ifcfg-eth0
|
||||
IPADDR=172.16.100.1
|
||||
NETMASK=255.255.255.0
|
||||
GATEWAY=172.16.100.25
|
||||
```
|
||||
|
||||
#### 2.元字符
|
||||
|
||||
元字符是这样一类字符,它们表达的是不同于字面本身的含义
|
||||
|
||||
shell元字符(也称为通配符)
|
||||
|
||||
正则表达式元字符
|
||||
|
||||
案例:
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# rm -rf *.pdf
|
||||
|
||||
[root@xingdian ~]# grep 'abc*' /etc/passwd
|
||||
abrt:x:173:173::/etc/abrt:/sbin/nologin
|
||||
```
|
||||
|
||||
#### 3.正则表达式元字符
|
||||
|
||||
基本正则表达式元字符:
|
||||
|
||||
```shell
|
||||
元字符 功能 示例
|
||||
^ 行首定位符 ^love
|
||||
$ 行尾定位符 love$
|
||||
. 匹配单个字符 l..e
|
||||
* 匹配前导符0到多次 ab*love
|
||||
.* 任意多个字符
|
||||
[] 匹配指定范围内的一个字符 [lL]ove
|
||||
[ - ] 匹配指定范围内的一个字符 [a-z0-9]ove
|
||||
[^] 匹配不在指定组内的字符 [^a-z0-9]ove
|
||||
\ 用来转义元字符 love\.
|
||||
\< 词首定位符 \<love
|
||||
\> 词尾定位符 love\>
|
||||
\(..\) 匹配稍后使用的字符的标签 :% s/172.16.130.1/172.16.130.5/
|
||||
:% s/\(172.16.130.\)1/\15/
|
||||
:% s/\(172.\)\(16.\)\(130.\)1/\1\2\35/
|
||||
:3,9 s/\(.*\)/#\1/
|
||||
|
||||
x\{m\} 字符x重复出现m次 o\{5\}
|
||||
x\{m,\} 字符x重复出现m次以上 o\{5,\}
|
||||
x\{m,n\} 字符x重复出现m到n次 o\{5,10\}
|
||||
```
|
||||
|
||||
扩展正则表达式元字符:
|
||||
|
||||
```shell
|
||||
+ 匹配一个或多个前导字符 [a-z]+ove
|
||||
? 匹配零个或一个前导字符 lo?ve
|
||||
a|b 匹配a或b love|hate
|
||||
() 组字符 loveable|rs love(able|rs) ov+ (ov)+
|
||||
(..)(..)\1\2 标签匹配字符 (love)able\1er
|
||||
x{m} 字符x重复m次 o{5}
|
||||
x{m,} 字符x重复至少m次 o{5,}
|
||||
x{m,n} 字符x重复m到n次 o{5,10}
|
||||
```
|
||||
|
||||
#### 4.正则匹配示例
|
||||
|
||||
```shell
|
||||
/love/
|
||||
/^love/
|
||||
/love$/
|
||||
/l.ve/
|
||||
/lo*ve/
|
||||
/[Ll]ove/
|
||||
/love[a-z]/
|
||||
/love[^a-zA-Z0-9]/
|
||||
|
||||
/.*/
|
||||
/^$/
|
||||
/^[A-Z]..$/
|
||||
/^[A-Z][a-z ]*3[0-5]/
|
||||
/[a-z]*\./
|
||||
/^[A-Z][a-z][a-z]$/
|
||||
/^[A-Za-z]*[^,][A-Za-z]*$/
|
||||
/\<fourth\>/
|
||||
/\<f.*th\>/
|
||||
/5{2}2{3}\./
|
||||
5{2}
|
||||
空行
|
||||
/^$/
|
||||
```
|
||||
|
||||
#### 5.正则案例
|
||||
|
||||
```shell
|
||||
#!/bin/bash
|
||||
read -p "please input number:" num
|
||||
if [[ ! "$num" =~ ^[0-9]+$ ]]
|
||||
then
|
||||
echo "error number!"
|
||||
else
|
||||
echo "is number!"
|
||||
fi
|
||||
```
|
||||
|
Loading…
Reference in New Issue