String handling
Substring interception
1.${变量名:起始位置(从0开始):长度}
[root@server0 ~]# phone=18676749523
[root@server0 ~]# echo $phone
18676749523
[root@server0 ~]# echo ${phone:0:5}
18676
[root@server0 ~]# echo ${phone:2:5}
67674
查看位数
[root@server0 ~]# echo ${#phone}
11
2.expr substr $变量名 起始位置 长度
[root@server0 ~]# expr substr $phone 1 5
18676
[root@server0 ~]# expr substr $phone 3 2
67
3.echo $变量名 | cut -b 起始位置-结束位置
杠号为2到4
[root@server0 ~]# echo $phone | cut -b 2-4
867
逗号为取该位置
[root@server0 ~]# echo $phone | cut -b 2,5,8
869
An example of string interception:
[Use a random number to take one of the digits [script]]
例:
#!/bin/bash
i='abcdefghijklmnopqrstuvwxyz1234567890zxcvbnmasdfghjkl'
n=$[RANDOM%53+1]
echo $i | cut -b $n
n=$[RANDOM%53] does not add 1 because it starts from 0
echo ${i:$n:1}
在变量i的后面继续取值
例:
[root@server0 /]# i=1
[root@server0 /]# p=$p$i
[root@server0 /]# echo $p
1
[root@server0 /]# i=0
[root@server0 /]# p=$p$i
[root@server0 /]# echo $p
10
替换
echo ${变量名/需替换的数字n/*} 替换第一个数字n为*
echo ${变量名//需替换的数字n/*} 替换所有的n为*
例:
[root@server0 /]# echo $phone
18676749525
[root@server0 /]# echo ${phone/8/}
1676749525
[root@server0 /]# echo ${phone/5/}
1867674925
[root@server0 /]# echo ${phone//5/}
186767492
[root@server0 /]# echo ${phone//495/}
186767*25
[root@server0 /]# echo ${phone//495/}
18676725
匹配删除
1.
echo ${变量名#*:}从开头一直删除到第一个:
echo ${变量名##*:}从开头删除到最后的:
例:
[root@server0 /]# n=head -1 /etc/passwd
[root@server0 /]# echo $n
root:x:0:0:root:/root:/bin/bash
[root@server0 /]# echo ${n#:}
x:0:0:root:/root:/bin/bash
[root@server0 /]# echo ${n##:}
/bin/bash
2.
echo ${变量名%:*}从后开始删除到:
echo ${变量名%%:*}从后开始删除所有到:
例:
[root@server0 /]# echo ${n%:}
root:x:0:0:root:/root
[root@server0 /]# echo ${n%%:}
root
利用匹配删除批量修改文件名:
#!/bin/bash
for i in ls *.jpg
[View files ending with jpg]
do
mv $i ./${i%.*}.doc [Change to ending with doc]
done
判断变量并赋值
echo ${variable1:-variable2} If variable 1 exists, display it, if not, display variable 2
例:
[root@server0 /]# echo ${who:-dc}
dc
[root@server0 /]# who=000
[root@server0 /]# echo ${who:-dc}
000
利用判断是否输入,无输入则执行100
#!/bin/bash
num=0
read -p "shu ru :" o
o=${o:-100}
for i in seq $o
do
let num+=i
done
echo $num
EXPECT自动交互
Simulate character operation
send email (non-interactive)
mail -s header root << EOF (end)
hehe
hehe
EOF
Use expect to automatically interactively create directories remotely
#!/bin/bash
expect << EOF
spawn ssh 192.168.4.10
expect "password" {send "123456\n"}
expect "#" {send "mkdir /xxx.x\n"}
expect "#" {send "exit\n"}
EOF
Step
1) Install expect software
2) Write configuration file
#!/bin/bash
expect << EOF
1>spawn ssh 192.168.4.10 [command line]
2>expect "password" {send "123456\n"} [command line]
3>expect "#" {send "mkdir / xxx.x\n"} 【command line】
4>expect "#" {send "exit\n"} 【last line not executed】
EOF
1>Remote to the machine of ip192.168.4.10
2>When you see the word "passsword", enter 123456
3>When you see "#", create a directory
4>Do not execute, prevent the last command from being executed
Reasons for failure to run:
1. Is expect installed ? 2. Specify
a program that needs
interaction
ssh remote without confirmation
ssh -o StrictHostKeyChecking=no
正则表达式
^ start
$ end
[] set, take any character in the set: grep [az] a.txt take any letter from a to z
[^] negate the set: [^abc] other than abc
. Any single character
-
The previous character appears any number of times [not allowed to be used alone]
. Any all == ( )
{n,m} The previous character appears n to m times
{n,} The previous character appears n or more times
{n } The previous character appears n times扩展正则表达式
-
Match at least once
? Match at most once
{n,m} Match n to m times
() Combine as a whole, preserving
| or
\b word boundariesFind the line containing the word init
egrep '\binit\b' /etc/rc.local
-