1、shell中的for语句
格式:
for 定义变量
do
active()
done
说明:for语句中定义变量的格式有多种:序列(seq 1 10);字符串(1 2 3);倒序(10...1);运算((i=0;i<3;i++));数组。
举例说明:
![](https://i-blog.csdnimg.cn/blog_migrate/3ad3acdb24d59dcb032003f0f91c82bc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6a3262358280a6f3eed81a2bd9758e38.png)
2、shell中的while语句
格式:
while 设定条件
do
active()
done
说明:这里是的条件是一个判断条件,true表示永远成立,或者设定test的条件进行对比;while条件正确才执行;
举例说明:
![](https://i-blog.csdnimg.cn/blog_migrate/a847f8bf4a718731b0809bb05372df49.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b22f7b66c8e6f1812a9475a2e4f5bcc9.png)
3、shell中的until语句
格式:
until false
do
active()
done
说明:这里until执行的内容为:错误的(即条件为假)
举例说明:因为这个和while正好相反,这里不再进行仔细演示。
4、shell中的if语句
格式:
if 条件
then
active()
elif 条件
then
active()
fi
说明:条件为真则相应执行下面的语句,依次执行,当遇到条件为正则执行完毕,或者执行到fi;
举例说明:
![](https://i-blog.csdnimg.cn/blog_migrate/68285c46ade00124b26324a571a1cf8b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6619982968fd702a7b4f4847214411e3.png)
5、shell中的case语句
格式:
case $1 in
world1)
active()
;;
world2)
active()
;;
esac
说明:其中world1 world2为匹配词;case原理根据匹配的词进行任务的执行,时并行关系;
举例说明:
![](https://i-blog.csdnimg.cn/blog_migrate/9b0234719f615eaea1793f4a54fbd9ea.png)
![](https://i-blog.csdnimg.cn/blog_migrate/57f55f71f02b4ac6191ec70f22e6c3e3.png)
6、shell中的expect语句
格式:
文件expect脚本内容格式:
#!/usr/bin/expect ###因为expect为单独的环境,所以设置运行环境
set timeout 1 ###设置回答问题的时间,超过该时间则继续下一项
set NAME [ lindex $argv 0 ] ###设置脚本运行时,后面所跟第一串字符内容,并命名为NAME
set NAME [ lindex $argv 0 ]
.....
spawn /***/**** ###将“问题脚本”(运行中需要回答问题),连接到“答案脚本”(回答问题的答案)
expect{ ###进行问题匹配,回答问题
"name" { send "NAME\r";exp_continue} ####“name”为问题匹配的关键词(唯一);send后面为所跟匹配答案;exp_continue 表示后面依然后问题答案,继续执行。
....
"name" { send "NAME\r";}
}
expect eof ###结束问题匹配
说明:
1. 含有回答问题的脚本需要执行权限,这样才可被使用;
2. 改脚本问题的执行命令为:expect,若系统中不存在,还需安装该脚本运行环境。
举例说明:
![](https://i-blog.csdnimg.cn/blog_migrate/54979535af12d054ef371bfce00da0d8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7025d925368353b8f4660be9bab7d23a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/14462756751dd7a2454939d86a0c5cc1.png)
7、shell中的三种中断
contiue: 终止当此次循环并继续后面循环;
break : 终止当前所在语句所有动作进行语句外的其他动作
exit: 退出脚本