执行流就是执行顺序
shell中默认执行顺序是从上到下依次执行
for语句
作用:为循环执行动作
for语句结构
for 定义变量
do 使用变量,执行动作
done 结束标志
for语句的基本格式
格式1:
#!/bin/bash
for I in $(seq 1 2 10)
do
echo $I
done
格式2:
for I in westos linux lee
do
echo $I
done
格式3:
for I in ${1..10}
do
echo $I
done
格式4:
for (( I=0; I<10; I++ ))
do
echo $I
done
脚本练习
check_host.sh
用此脚本检测 10 台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的 ip 列表
条件语句
while ... do 语句
作用:条件为真执行动作
语句结构:while ture
do
done
until...do 语句
作用:条件为假执行动作
语句结构
until false
do
done`if...then...elif...then...else...fi 语句
作用:多次判定条件执行动作
语句结构:
if
then
elif
then
...
else
fi
脚本练习
sh create_filetype.sh
如果后面没有跟文件,显示Please input filename to check
sh create_filetype.sh file
如果文件不存在,显示file is not exist
如果文件是普通文件,显示file is common file
如果是目录,显示file is directory
如果是个链接,显示file is link
case语句
case $1 in
word1 | WORD1)
action1
;;
word2 | WORD2)
action2
;;
*)
action3
esac
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 "$NAMEr";exp_continue }
"old" { send "$AGEr";exp_continue }
"objective" { send "$OBJr";exp_continue }
"ok" { send "$FEELr" }
}
expect eof
脚本练习
host_list.sh
检测 172.25.254.1-172.25.254.10 网络是否开启
如果网络正常请生成解析列表 hosts_list
格式如下
ip 主机名称
例如 : 172.25.254.1 为开启状态主机名为 westos_student1.westos.org
hosts_list 中
172.25.254.1 westos_student1.westos.org
break,continue,exit
contiue 终止当此次前循环提前进入下个循环
break 终止当前所在语句所有动作进行语句外的其他动作
exit 脚本退出



