1.for语句
• for NUM in 1 2 3
• for NUM in {1..3}
• for NUM in seq 1 3 或者 for NUM in seq 1 2 10
• do
• done
for语句示例
• for NAME in westos linux 666
do
echo $NAME
done
2.while语句
• while 条件
• do
• done
while示例
• while true
do
echo -n uptime > /dev/Devttyecho−ne“\r\r”>/dev/Dev_tty
sleep 2
done
3.if语句
• if
• then
• elif
• then
• 。。。
• else
• fi
if语句示例
• if [ “1”==”start”]thensystemctlstart2
elif [ “1”==“stop”]thensystemctlstop2
else
echo “error:please input start or stop after scripts!”
fi
4.case语句
• case
word1)
action1
;;
word2)
action2
;;
……..
*)
action_last
esac
case语句示例
• case $1 in
westos)
echo linux
;;
linux)
echo westos
;;
*)
echo “Error: input westos or linux after script !!”
esac
5.expect
• expect是自动应答命令用于交互式命令的自动执行
• spawn是expect中的监控程序,其运行后会监控命令提出的交互问题
• send 发送问题答案给交互命令
• “\r” 表示回车
• exp_continue 标示当问题不存在时继续回答下面的问题
• expect eof 标示问题回答完毕退出expect环境
• interact 标示问题回答完毕留在交互界面
• set NAME [ lindex $argv n ] 定义变量
expect示例
ask.sh
!/bin/bash
read -p “what’s your name: ” NAME
read -p “How old are you: ” AGE
read -p “Which class you study: ” CLASS
read -p “You feel happy or terrible ?” FEEL
echo NAMEisAGE\’s old and NAMEisfeelFEEL
chmod +x ask.sh
!/bin/bash
expect <
6.脚本中的语句控制器
• exit n 脚本退出,退出值为n
• break 退出当前循环
• continue 提前结束循环内部的命令,但不终止循环
278

被折叠的 条评论
为什么被折叠?



