Shell Programming (4)

 

shell functions

 

It allows the shell or set of statements is formed a set of commands usable code section, code blocks called shell functions. This code is called a name to the function name, you can call the subsequent sections of the code directly.

 

format

func () {# function name specified 

command # function body 

}

 

Example 1:

Copy the code
#!/bin/bash
func() {
echo "This is a function."
}
func

# bash test.sh
This is a function.
Copy the code

Shell function is very simple, function name followed by a double brackets, braces double talk. Direct call by the function name, without parentheses.

 

Example 2: Function Return Value

Copy the code
#!/bin/bash
func() {
VAR=$((1+1))
return $VAR
echo "This is a function."
}
func
echo $?

# bash test.sh
2
Copy the code

return value of the function is defined to return status back and termination functions , but the numbers can only be returned 0-255, similar exit.

 

Example 3: Function parameter passing

Copy the code
#!/bin/bash
func() {
echo "Hello $1"
}
func world
# bash test.sh
Hello world
Copy the code

Shell parameter passing through the position parameter to the function.

 

shell regular expressions

 

Regular expressions in each language will have, function is to match your expectations in line with string.

Shell Regular expressions are divided into two types:

  •  Basic regular expressions
  • Extended regular expressions: the expression has extended +,, | and ()?

 

  1. Regular expression is to handle large volumes of text | set of rules and methods defined strings
  2. These special auxiliary symbols by definition, the system administrator can quickly filter, replace or string desired output. Linux Regular expressions are generally processed in units.

 

Regular expressions and wildcards are essentially different

 

 

  1. Judgment method does not require thinking: the Three Musketeers awk, sed, grep, egrep are regular, others are wildcards
  2. The difference between regular expressions and wildcards easiest way:

(1) file directory name ===> wildcard
              (2) file content (string [file] text content) ===> Regular Expressions

 

Here are some common regular expression notation, we acquire a grep tool to illustrate.

Note: In match mode must be in quotes

 

                  symbol                         description                                Examples
. Matches any single character (must be present) Examples: l..e

 

It can be expressed

love

like

LEEE

Not represented

labcde

the

lee

^ The beginning of the match before the string Matching lines that begin with abc:

 

echo -e “abc\nxyz” |grep ^abc

$ Matching the front end of the string Xyz matching lines to the end:

 

echo -e “abc\nxyz” |grep xyz$

* Matches the preceding character zero or more a * indicates a situation any number of

 

a*b 表示b前面有任意个a的情况(包括没有a的情况)

.* 
表示任意长度的任意字符 例子:过滤出一行中a在前,b在后的行

 

条件:

包含 a 和 b

字母 a 必须在 b前面

 

# grep –color “a.*b” b.txt

+(扩展正则) 表示其前面的字符出现最少一次的情况 匹配 abc 和 abcc:

 

echo -e “abc\nabcc\nadd” |grep -E ‘ab+’

匹配单个数字:echo “113” |grep -o ‘[0-9]’

连续匹配多个数字:echo “113” |grep -E -o ‘[0-9]+’

?(扩展正则) 表示其前面的字符出现最多一次的情况(可以0个) 匹配 ac 或 abc:

 

echo -e “ac\nabc\nadd” |grep -E ‘a?c’

[] 表示范围内的一个字符 例子:过滤出包含小写字母的行       grep [a-z] a.txt

 

例子:过滤出包含大写字母的行       grep [A-Z] a.txt

例子:过滤出包含数字的行           grep [0-9] a.txt

例子:过滤出包含数字和小写字母的行 grep [0-9a-z] a.txt

例子:过滤出包含字母asf的行        grep [asf] a.txt

[ .-.] 匹配中括号中范围内的任意一个字符 匹配所有字母

 

echo -e “a\nb\nc” |grep ‘[a-z]’

[^] 匹配[^字符]之外的任意一个字符 匹配 a 或 b:

 

echo -e “a\nb\nc” |grep ‘[^c-z]’

匹配末尾数字:echo “abc:cde;123” |grep -E

‘[^;]+$’

^[^] 匹配不是中括号内任意一个字符开头的行 匹配不是#开头的行:

 

grep ‘^[^#]’ /etc/httpd/conf/httpd.conf

{n}或者{n,} {n}:表示严格匹配n个字符

 

{n,}匹配花括号前面字符至少 n个字符

echo “aadadccc” | egrep “a{2}”

 

echo “aadadccc” | egrep “a{1}”

{n,m} 匹配花括号前面字符至少 n个字符,最多 m 个字符 例子:

 

“ac\{2,5\}b” 匹配a和b之间有最少2个c最多5个c的行

“ac\{,5\}b” 匹配a和b之间有最多5个c的行

“ac\{2,\}b” 匹配a和b之间有最少2个c的行

\<  锚定单词首部(单词一般以空格或特殊字符做分隔) # echo “hi,root,iamroot” | grep “\<root”
hi,root,iamroot
# echo “hi,root,iamroot” | grep “root\>”
hi,root,iamroot
# echo “hi,root,iamroot” | grep “\<root\>”
hi,root,iamroot

 

 

\>  锚定单词尾部(单词一般以空格或特殊字符做分隔,) # echo “hi,root,iamroot” | grep “\<root”
hi,root,iamroot
# echo “hi,root,iamroot” | grep “root\>”
hi,root,iamroot
# echo “hi,root,iamroot” | grep “\<root\>”
hi,root,iamroot
() \1  调用前面的第一个分组  

 

例子:过滤出一行中有两个相同数字的行

# grep “\([0-9]\).*\1” inittab

 

例子:过滤出行首和行位字母相同的行

# grep “^\([a-z]\).*\1$” inittab

 

|(扩展正则) 匹配竖杠两边的任意一个  

 

例子:过滤出cat 或者Cat

 

# grep “cat|Cat” a.txt

 

# grep “(C|c)at” a.txt

 

 

总结

 

正则表达式

 

一、字符匹配

.

[]

[^]

 

二、次数匹配

*

\{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:]

Copy the code
[root@ken ~]# cat test    #文本内容
#!/bin/bash
if [ 1 -eq 1 ];
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"
Copy the code

 

正则练习

 

Use file /etc/init.d/functions, the following may be some part of the title match of the case did not match.

1. The filtered line containing capital letters

2. Matching non-numeric characters

3. filtered in a first row, b in the row

There are a minimum of two up to 5 c 4 c is between a and b match line

5. filtered to beginning with #, and the second character is line with spaces

6. The same line of travel beginning of the line filter letters

7. filtered # is the first character and the second character string is not empty, and the end of the digital line

8. The filtered row line containing the same number of /etc/init.d/functions

 

 

 

 

answer:

1.

[root@ken ~]# grep "[A-Z]"  /etc/init.d/functions

 

2.

[root@ken ~]# grep "[^0-9]"  /etc/init.d/functions

 

3.

[root@ken ~]#  grep  "a.*b" /etc/init.d/functions

 

4. (not match)

[root@ken ~]# grep "ac\{2,5\}b" /etc/init.d/functions

 

5.

[root@ken ~]#  grep "^#[[:space:]]" /etc/init.d/functions

 

6. (not match)

[root@ken ~]# grep "^\([a-z]\).*\1$" /etc/init.d/functions

 

7. (not match)

[root@ken ~]# grep  "^#[^[:space:]].*[0-9]$" /etc/init.d/functions

 

8.

[root@ken ~]# egrep ".*([0-9]).*\1" /etc/init.d/functions

 

Guess you like

Origin www.cnblogs.com/it-peng/p/11404447.html