1、脚本中的函数
脚本中的函数是把一个复杂的语句块i定义成一个简单的字符串的方法。
该字符串为函数名
通过函数名来调用函数
需要先进行函数的定义,然后才能调用该函数
函数的定义
函数名称()
{
语句块
}
#!/bin/bash
FILE_TYPE() #定义函数
{
[ -z "$1" ] && {
echo "error please input the filename folowing $0"
exit
}
[ ! -e "$1" ] && {
echo " error $1 is not exist"
exit
}
[ -f "$1" ] && {
echo "the file is a nomal file"
}
[ -b "$1" ] && {
echo "the file is a block device"
}
[ -S "$1" ] && {
echo "the file is a socket"
}
}
FILE_TYPE # 通过函数名称调用函数
2、for 语句
循环语句
for 循环条件
do 执行的动作
done 循环结束标志
循环条件:会依次执行满足的每一个条件,规定循环的次数。
1 num in 1 2 3
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in 1 2 3 # num 依次为1 2 3
do
echo $num # 指定的动作是输出num的值
done
[root@localhost mnt]# sh test.sh # 执行脚本输出结果
1
2
3
2 num in {1…3}
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in {1..3}
do
echo $num
done
[root@localhost mnt]# sh test.sh
1
2
3
3 num in $(seq 1 3)
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in `seq 1 3`
do
echo $num
done
[root@localhost mnt]# sh test.sh
1
2
3
4 seq 存在一个好处 是可以指定步长。
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in $(seq 1 2 10) # 从1开始 步长为2 到10 结束
do
echo $num
done
[root@localhost mnt]# sh test.sh # 输出就是相差2
1
3
5
7
9
5 num的值不一定是数字,也可以是字符
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in lala toto lele haha # 设定区取值范围为几个字符串
do
echo $num
done
[root@localhost mnt]# sh test.sh # 运行依次输出这几该字符串
lala
toto
lele
haha
应用示例
10秒倒计时
#!/bin/bash
for num in {10..1}
do
echo -n " $num "
echo -ne "\r"
sleep 1
done
3、exit、 break、continue
结束的三种方式:
exit :直接结束脚本运行
break:跳出所在的循环语句 继续进行脚本
continue:结束本次循环,进行下次循环
test.sh 脚本正常运行是以下情况,分别在num为3 的使用结束 查看对比效果。
正常运行
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in {1..5}
do
echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh
1
2
3
4
5
hello welcome
exit
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in {1..5}
do
[ "$num" = '3' ] && exit # exit
echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh # 直接结束脚本的运行
1
2
break
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in {1..5}
do
[ "$num" = '3' ] && break # break
echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh
1
2
hello welcome # 结束循环 但是循环之后的语句还是要正常执行
continue
[root@localhost mnt]# cat test.sh
#!/bin/bash
for num in {1..5}
do
[ "$num" = '3' ] && continue #continue
echo $num
done
echo "hello welcome "
[root@localhost mnt]# sh test.sh
1
2
4 # 只是跳过num为3 那一次循环 继续进行下一次循环以及后续的语句
5
hello welcome
4 、if语句
主要用于进行条件判断
if 语句语法格式:
if 跟条件
then 跟上述条件满足所要执行的动作
fi if语句结束标志
示例:
[root@localhost mnt]# cat test.sh
#!/bin/bash
A=$1
B=$2
if
[ "$A" = "$B" ]
then
echo "yes"
fi
[root@localhost mnt]# sh test.sh 2 2 # 满足条件执行
yes
[root@localhost mnt]# sh test.sh 2 3 # 不满足条件不执行
if else 语法格式:
if
条件1
then
条件1满足所要执行的动作
else
其他情况下执行的动作
fi if语句结束标志
示例:
[root@localhost mnt]# cat test.sh
#!/bin/bash
A=$1
B=$2
if
[ "$A" = "$B" ]
then
echo "yes"
else
echo "no"
fi
[root@localhost mnt]# sh test.sh 2 2
yes
[root@localhost mnt]# sh test.sh 2 1
no
[root@localhost mnt]# sh test.sh 2 3
no
if else-if else 语法格式:适用于判断的条件较多的时候
if
条件1
then
条件1满足所要执行的动作
elif
条件2
then
条件2 所要执行的动作
elif
条件3
then
条件3 所要执行的动作
......
else
其他情况下执行的动作
fi if语句结束标志
示例:
[root@localhost mnt]# cat test.sh
#!/bin/bash
A=$1
B=$2
if
[ "$A" = "$B" ]
then
echo "等于"
elif
[ "$A" -gt "$B" ]
then
echo "大于"
else
echo "小于"
fi
[root@localhost mnt]# sh test.sh 1 2
小于
[root@localhost mnt]# sh test.sh 1 1
等于
[root@localhost mnt]# sh test.sh 2 1
大于
5、while 语句
while循环用于不断执行一系列命令
其格式为:
while 条件
do
动作
done #结束标志
示例:
[root@localhost mnt]# cat test.sh
#!/bin/bash
i=0
while [ "$i" -ne "10" ] # 进行循环的条件
do
echo $i # 执行的动作
((i+=1)) # i 自加1
done
[root@localhost mnt]# sh test.sh 运行循环 直到条件不满足结束
0
1
2
3
4
5
6
7
8
9
6、until 语句
until 循环执行一系列命令直至条件为 true 时停止。
until 循环与 while 循环在处理方式上刚好相反。
一般 while 循环优于 until 循环,极少数情况下,until 循环更加有用。
其格式为:
until 条件
do
动作
done #结束标志
示例:
[root@localhost mnt]# cat test.sh
#!/bin/bash
i=15
until [ "$i" -lt "10" ]
do
echo $i
((i--))
done
[root@localhost mnt]# sh test.sh
15
14
13
12
11
10
7 、case 语句
case语句为多选择语句,可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。
case 语句在进行判断的时候是并发进行的,处理效率较高,if 语句在进行条件判断的时候是逐条从上往下,处理效率较低。
case语句格式如下:
case 变量 in
情况1)
命令
;;
情况2)
命令
;;
......
*) #剩余的所有情况
命令
esac
示例
#!/bin/bash
discrip() # 定义函数
{
echo "action C|B|S|O "
read -p "please input the action following $0 : " ACTION
case $ACTION in # case 语句
c|C)
echo "$ACTION is create !!"
;;
b|B)
echo "$ACTION is backup !!"
;;
S|s)
echo "$ACTION is skip !!"
;;
o|O)
echo "$ACTION is overwrite !!"
;;
e|E)
exit
;;
*)
echo "error : $ACTION is unkown action !!"
esac
}
while true
do
discrip # while 语句调用函数
done
[root@localhost mnt]# sh test.sh
action C|B|S|O
please input the action following test.sh : g
error : g is unkown action !!
action C|B|S|O
please input the action following test.sh : t
error : t is unkown action !!
action C|B|S|O
please input the action following test.sh : c
c is create !!
action C|B|S|O
please input the action following test.sh : r
error : r is unkown action !!
action C|B|S|O
please input the action following test.sh : o
o is overwrite !!
action C|B|S|O
please input the action following test.sh : s
s is skip !!
action C|B|S|O
please input the action following test.sh : e
8、expect
expect 相对于语句,更像是一条命令。
expect 是自动应答命令用于交互式命令的自动执行
spawn 是expect中的监控程序,其运行后会监控命令提出的交互问题
send 发送问题答案给交互命令
exp_continue 表示当问题不存在时继续回答下面的问题
expecte of 表示问题回答完毕退出expect环境
interact 表示问题回答完毕留在交互界面
set NAME [ lindex $argvn ] 定义变量
yum install expect.x86_64 -y # 安装软件 ,才能使用 expect
expect脚本一般以#!/usr/bin/expect 开头 规定其运行环境
spawn 新建一个进程,这个进程的交互由expect控制
示例
1 存在一个交互式的脚本,
#!/bin/bash
read -p " who are you ? " NAME
read -p " where are you from ? " ADDR
read -p " how old are you ? " AGE
read -p " waht are you doing ? " ACTION
echo "$NAME is from $ADDR,now $AGE yeas old ,$ACTION "
2 在运行该脚本的时候需要一步一步的输入所要的参数
[root@localhost mnt]# sh question.sh
who are you ? shang
where are you from ? xi'an
how old are you ? 18
waht are you doing ? study
shang is from xi'an,now 18 yeas old ,study
3 使用expect 可以一次回答所有的问题。
vim anwser.exp # 注意文件后缀 便于区分
#!/usr/bin/expect # 注意规定运行环境
set NAME [ lindex $argv 0 ]
set ADDR [ lindex $argv 1 ]
set AGE [ lindex $argv 2 ]
set ACTION [ lindex $argv 3 ] # 定义四个变量
spawn /mnt/question.sh # 监控需要进行交互式的脚本
expect {
"who" { send "$NAME\r";exp_continue }
"where" { send "$ADDR\r";exp_continue } exp_continue 表示当问题不存在时继续回答下面的问题
"how" { send "$AGE\r";exp_continue }
"what" { send "$ACTION\r" } # 根据问题发送答案
}
expect eof expect eof 表示问题回答完毕退出expect环境
4 执行
chmod 777 /mnt/question.sh # 被检控的文件必须有可执行权限
[root@localhost mnt]# expect anwser.exp shang xian 18 study # 运行expect 后面跟这问题的答案
spawn /mnt/question.sh
who are you ? shang
where are you from ? xian
how old are you ? 18
what are you doing ? study
shang is from xian , now 18 yeas old, study !! # 一次回答所有的问题并输出结果!
5 也可以将 anwser.exp 修改成可以运行在shell 脚本中
修改后的内容
#!/bin/bash
/usr/bin/expect <
执行结果
[root@localhost mnt]# sh anwser.sh shang 18 xian study
spawn /mnt/question.sh
who are you ? shang
where are you from ? 18
how old are you ? xian
what are you doing ? study
shang is from 18 , now xian yeas old, study !!