1. shell函数
2. shell正则表达式
shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数。给这段代码起个名字称为函数名,后续可以直接调用该段代码。
格式
func() { #指定函数名
command #函数体
}
实例1:
#!/bin/bash
func() {
echo "This is a function."
}
func
# bash test.sh
This is a function.
# bash test.sh
This is a function.
Shell 函数很简单,函数名后跟双括号,再跟双大括号。通过函数名直接调用,不加小括号。
示例 2:函数返回值
#!/bin/bash
func() {
VAR=$((1+1))
return $VAR
echo "This is a function."
}
func
echo $?
# bash test.sh
2
return 在函数中定义状态返回值,返回并终止函数,但返回的只能是 0-255 的数字,类似于 exit。
示例 3:函数传参
#!/bin/bash
func() {
echo "Hello $1"
}
func world
# bash test.sh
Hello world
通过 Shell 位置参数给函数传参。
正则表达式在每种语言中都会有,功能就是匹配符合你预期要求的字符串。
Shell 正则表达式分为两种:
(1)文件目录名===>通配符
(2)文件内容(字符串,文本【文件】内容)===>正则表达式
下面是一些常用的正则表达式符号,我们先拿 grep 工具举例说明。
注意:在匹配模式中一定要加上引号
例子1:
[root@ken ~]# cat test
ac
abc
abbc
anmcc
[root@ken ~]# grep “a.c” test
abc
[root@ken ~]# grep -o “a..c” test
abbc
anmc
例子1:匹配以root开头的行
[root@ken ~]# cat test1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@ken ~]# grep “^root” test1
root:x:0:0:root:/root:/bin/bash
[root@ken ~]# grep “^r” test1
root:x:0:0:root:/root:/bin/bash
例子2:找到所有#号开头的行
[root@ken ~]# grep “^#” test1
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
[root@ken ~]# grep “bash$” test1
root:x:0:0:root:/root:/bin/bash
例子2:找到所有空白行
[root@ken ~]# vim test1
[root@ken ~]# grep “^$” test1
例子1:
[root@ken ~]# cat test | grep -o “a*b”
ab
ab
b
aab
aaab
aaaaab
b
例子2:
[root@ken ~]# cat test
abc
abbc
aab
aaab
aaaaab
acab
[root@ken ~]# cat test | grep -o “a*b”
ab
ab
b
aab
aaab
aaaaab
ab
例子3:
[root@ken ~]# cat test
abc
abbc
aab
aaab
aaaaab
acab
[root@ken ~]# grep -o “a.*b” test
ab
abb
aab
aaab
aaaaab
acab
例子1:
[root@ken ~]# cat test | grep -o “[ab]”
a
b
a
b
b
a
a
b
a
a
a
例子2:匹配以字母开头的行
[root@ken ~]# cat test1 | grep “^[a-z]”
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin
例子3:
[root@ken ~]# cat test1 | grep “^[a-zA-Z]”
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Ooperator:x:11:0:operator:/root:/sbin/nologin
例子4:
[root@ken ~]# cat test1 | grep “^[a-zA-Z0-9]”
root:x:0:0:root:/root:/bin/bash
3bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Ooperator:x:11:0:operator:/root:/sbin/nologin
例子1:
[root@ken ~]# cat test1 | grep “^[^a-z]”
3bin:x:1:1:bin:/bin:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
$mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Ooperator:x:11:0:operator:/root:/sbin/nologin
7.锚定 单词首部:\< 特殊字符,空格
锚定单词尾部: \>
例子1:
[root@ken ~]# cat test1 | grep “\<root”
root:x:0:0:root:/root:/bin/bash
Ooperootrator:x:11:0:operator:/root:/sbin/nologin
例子2:
[root@ken ~]# cat test1 | grep “\<root\>”
root:x:0:0:root:/root:/bin/bash
rootooperootrator:x:11:0:operator:/root:/sbin/nologin
注意:基础正则:中的{}。以及()前面都要加上使用格式:\{\} \(\)
8. \{m,n\} 表示匹配前面的字符出现至少m次,至多n次
例子1:
[root@ken ~]# cat test | grep -o “a\{1,2\}b”
ab
ab
aab
aab
aab
ab
例子2:
[root@ken ~]# cat test | grep -o “a\{1,\}b”
ab
ab
aab
aaab
aaaaab
ab
9. \(\) \1表示的对某个单词进行分组,\1表示对第一个分组进行调用
[root@ken ~]# sed -i ‘s/\(SELINUX=\)disabled/\1enforcing/g’ /etc/sysconfig/selinux
##扩展正则使用:
1.egrep
2.grep -E
10.+ 表示前面的字符出现一次的情况
例子1:
[root@ken ~]# cat test | egrep “a+b”
abc
abbc
aab
aaab
aaaaab
acab
例子2:
[root@ken ~]# cat test | grep -E “a+b”
abc
abbc
aab
aaab
aaaaab
acab
11.| 或
例子1:
[root@ken ~]# echo “cat jdkajk Cat” | grep -E “(cat)|(Cat)”
cat jdkajk Cat
例子2:
[root@ken ~]# echo “cat jdkajk Cat” | grep -E “(c|C)at”
cat jdkajk Cat
12.?表示前面的字符出现最多一次的情况
例子1:
[root@ken ~]# cat test | grep -E -o “a?b”
b
ab
ab
b
ab
ab
ab
ab
一、字符匹配
.
[]
[^]
二、次数匹配
*
\{m,n\}
`
三、锚定
^
$
\<
\>
四、分组
\(\)
\1
grep -E
egrep
一、字符匹配
.
[]
[^]
二、次数匹配
*
{m,n}
+?表示其前面的字符出现最少一次的情况
?表示其前面的字符出现最多一次的情况
三、锚定
^
$
\<
\>
四、分组
()
\1
\2
五、或
|
一.、正则表达式中的{}以及()都需要加上\进行转义,而扩展正则表达式不需要
二 、|, ?,+是扩展正则独有的
三、 锚定单词首部和尾部在扩展正则以及正则中都需要加上
```
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Posix字符 | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ?描述 |
| [:alnum:] | ?
等效a-zA-Z0-9
|
| [:alpha:] | 等效a-zA-Z |
| [:lower:] | 等效a-z |
| [:upper:] | 等效A-Z |
| [:digit:] | 等效0-9 |
| [:space:] | 匹配任意空白字符,等效\t\n\r\f\v |
| [:graph:] | 非空白字符 |
| [:blank:] | 空格与定位字符 |
| [:cntrl:] | 控制字符 |
| [:print:] | 可显示的字符 |
| [:punct:] | 标点符号字符 |
| [:xdigit:] | 十六进制 |
注意:使用这些字符的时候需要在外面还要加一个[]括号
说一下[:space:]
[root@ken ~]# cat test #文本内容
then echo "yes"
else echo "no"
fi
AJDLAJDL
LAJLDJA
JDKAJkjskdjklaskj
lsdjal0dlkakm
[root@ken ~]# grep ‘[[:space:]]‘ test #过滤出包含空格的行,[:space:]
括号外面还要再包含一个[] if [ 1 -eq 1 ];
then echo "yes"
else echo "no" [root@ken ~]# grep ‘ ‘ test #也可以使用一个空格来代替[:space:] if [ 1 -eq 1 ];
then echo "yes"
else echo "no"
```
使用文件 /etc/init.d/functions ,下面可能有些部分题目匹配不到符合的情况。
1.?过滤出包含大写字母的行
2.?匹配非数字字符
3.?过滤出一行中a在前,b在后的行
4.?匹配a和b之间有最少2个c最多5个c的行
5.?过滤出以#?为开头,且第二个字符是空格的行
6.过滤出行首和行位字母相同的行
7.过滤出第一个字符是#,且第二个字符串是非空字符,而且结尾是数字的行
8.过滤出一行包含相同数字的行/etc/init.d/functions
答案:
[root@ken ~]# grep "[A-Z]" /etc/init.d/functions
[root@ken ~]# grep "[^0-9]" /etc/init.d/functions
[root@ken ~]# grep "a.*b" /etc/init.d/functions
4.(匹配不到)
[root@ken ~]# grep "ac{2,5}b" /etc/init.d/functions
6.(匹配不到)
[root@ken ~]# grep "^([a-z]).*\1$" /etc/init.d/functions
7.(匹配不到)
[root@ken ~]# grep "^#[^[:space:]].*[0-9]$" /etc/init.d/functions
原文:https://www.cnblogs.com/LibetJohn/p/11126575.html