1 for循环
for循环的格式
for ##定义变量
do ##使用变量,执行动作
done ##结束标志
(1)格式1
#!/bin/bash
for i in 1 2 3
do
echo $i
done
(2)格式2
#!/bin/bash
for i in {1..3}
do
echo $i
done
(3)格式3
#!/bin/bash
for i in {3..1}
do
echo $i
done
(4)格式4
#!/bin/bash
for i in `seq 1 2 7` ## 从1遍历到7,步长为2,如果不设置步长则默认为1
do
echo $i
done
(5)格式5
#!/bin/bash
for ((i=1;i<4;i++)) ## i的初始值为1,如果i<4执行循环,同时i+1
do
echo $i
done
脚本练习:check_host.sh
- 用此脚本检测10台与您当前主机直连主机是否网络通常 如果网络通常请显示主机的ip列表
#!/bin/bash
for i in {1..10}
do
ping -w1 172.25.254.$i &>/dev/null && echo 172.25.254.$i
done
2 while语句
(1)while的格式
while ture ##条件为真
do ##条件成立所作循环动作
done ##结束标志
编写脚本:实现根据提示输入字符串时,输出相应的字符串
#!/bin/bash
while true
do
read -p "please input word: " WORD
echo " the word is: " $WORD
done
3 until
(1)格式
until false ##条件为假
do
##条件不成立所执行的循环动作
done ##结束标志
练习:
#!/bin/bash
read -p "please input word: " WORD
until [ "$WORD" != "exit" ] ##输入exit时执行do的语句
do
echo bye
exit
done
4 if语句
(1)if控制流的格式
if ##条件1
then ##满足条件1执行的语句
elif ##条件2
then ##满足条件2执行的语句
...
else ##其他条件
fi ##结束标志
测试题:
创建脚本:,满足下列条件:
脚本执行后界面输出:please input filename: file
如果文件不存在界面输出提示:file is not exist
如果文件存在输出文件的类型:file is file
如果脚本的判断中没有检测的文件的类型则输出unknow
此脚本会一直询问直到用户输入exit为止
#!/bin/bash
while true
do
read -p "please input filename: " FILENAME
if [ "$FILENAME" = "exit" ]
then
exit ##输入为exit退出程序
else
if [ ! -e "$FILENAME" ]
then
echo "$FILENAME is not exit" ##文件不存在时输出提示,跳出本次的if语句
elif [ -S "$FILENAME" ]
then
echo "$FILENAME is socket "
elif [ -L "$FILENAME" ]
then
echo "$FILENAME is link "
elif [ -f "$FILENAME" ]
then
echo "$FILENAME is common file "
elif [ -b "$FILENAME" ]
then
echo "$FILENAME is block "
elif [ -c "$FILENAME" ]
then
echo "$FILENAME is character device"
elif [ -d "$FILENAME" ]
then
echo "$FILENAME is direct"
else
echo unknow ##若文件存在且不是以上的文件类型时输出unknow
fi
fi
done
脚本执行效果:
5 case
case的格式:
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
7 结束循环
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##退出
for i in {1..10}
do
[ "$i" = "6" ] && {
echo 6 is continue
exit/break/continue
}
echo $i
done
echo haha
(1)exit
(2)break
(3)countinue
8 expect
expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信。
expect自动交互流程:spawn启动指定进程—expect获取指定关键字—send向指定程序发送指定字符—执行完成退出
(1)下载expect
(2)expect的常用命令
spawn 交互程序开始后面跟命令或者指定程序
expect 获取匹配信息匹配成功则执行expect后面的程序动作
send exp_send 用于发送指定的字符串信息
exp_continue 在expect中多次匹配就需要用到
send_user 用来打印输出 相当于shell中的echo
exit 退出expect脚本
eof expect执行结束 退出
set 定义变量
puts 输出变量
set timeout 设置超时时间
(3)示例
(1)问题脚本
#!/bin/bash
read -p "what's your name:" NAME
read -p "How old are you: " AGE
read -p "Which objective: " OBJ
read -p "Are you ok? " OK
echo $NAME is $AGE\'s old study $OBJ feel $OK
(2)应答脚本
#!/usr/bin/expect
set timeout 1
set NAME [ lindex $argv 0 ]
set AGE [ lindex $argv 1 ]
set OBJ [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]
spawn /mnt/ask.sh
expect {
"name" { send "$NAME\r";exp_continue }
"old" { send "$AGE\r";exp_continue }
"objective" { send "$OBJ\r";exp_continue }
"ok" { send "$FEEL\r" }
}
expect eof
(3)用函数实现应答
#!/bin/bash
EXPECT(){
/usr/bin/expect <<EOF
spawn sh /mnt/test.sh
expect {
"name" { send "$1\r";exp_continue }
"old" { send "$2\r";exp_continue }
"objective" { send "$3\r";exp_continue }
"ok" { send "$4\r" }
}
expect eof
EOF
}
EXPECT $1 $2 $3 $4