##1.for语句##
(1)for语句作用
为循环执行动作
(2)for语句结构
for 定义变量
do 使用变量,执行动作
done 结束标志
(3)for语句的基本格式
格式1
格式1:`#!/bin/bash` 
`for WESTOS in $(seq 1 2 10)` 
`do` 
`echo $WESTOS` 
`done` 

格式2
格式2:
`for WESTOS in westos linux lee` 
`do` 
`echo $WESTOS` 
`done` 

格式3
格式3:
`for WESTOS in {10..1}`
`do` 
`echo $WESTOS` 
`done` 
 


格式4
格式4: 
`for ((WESTOS=0;WESTOS<10;WESTOS++))` 
`do` 
`echo $WESTOS` 
`done`



脚本练习:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表
##2.条件语句##
(1)while...do语句
######作用:
条件为真执行动作
######语句结构:
`while ture` <!--条件为真-->
`do` <!--条件成立所做循环动作-->
`done` <!--结束-->


(2)until...do 语句
######作用:
条件为假执行动作
######语句结构:
`until false` <!--条件为假-->
`do` <!--条件不成立所做循环动作-->
`done` <!--结束-->


(3)if...then...elif...then...else...if 语句
######作用:
多次判定条件执行动作
######语句结构:
`if` <!--首次判定-->
`then` <!--条件成立执行动作-->
`elif` <!--当首次判定不成立时再次判定-->
`then` <!--条件成立执行动作-->
`...` <!--else if可以执行多次-->
`else` <!--所有条件不成立执行动作-->
`fi` <!--结束-->
 
 
#### 脚本练习:
user.sh判断用户类型
please input username: linux
linux不存在时请输出
please input password:123
建立linux用户并设定密码为123
用户存在时判断用户类型
此脚本会一直询问直到用户输入exit为止
##3.case##
case $1 inword1|WORD1) action1 ;;word2|WORD2)action2 ;; *)action3 
esac 

######脚本练习:
输入userctrl add
Please input username:lee
lee is exist #用户已存在
Please input passsword:123 ##用户不存在
lee is created
输入userctrl del
Please input username:lee
lee is not exist #用户不存在
Please input passsword:123 #用户已存在
lee is deleted
如果输入没加add或del
ERROR:Please input add|del following userctrl!!
##4.expect##
问题脚本
#!/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 
 
应答脚本
#!/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 
 

将expect和shell环境融合在一起:


脚本练习
host_name.sh
检测192.168.1.1-192.168.1.10网络是否开启
如果网络正常请显示主机名
格式如下
ip 主机名称
例如:192.168.1.1为开启状态主机名为westos_student1.westos.org
显示westos_student1.westos.org

##5.脚本中的控制器##


contiue ##终止当次循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出
return ##提前结束函数,函数之外依然进行




 
 
 
 




