1、for…do循环语句
for语句的作⽤为循环执⾏指定动作,其语句结构如下:
for 定义变量
do 使⽤变量,执⾏动作
done 结束标志
for语句的基本格式有以下四种:
1)in后使用罗列方法定义变量值
示例:
for WESTOS in westos linux lee
do
echo $WESTOS
done
2)in后如果变量值连续,可以使用{起始值…结束值}定义变量值
示例:
for WESTOS in {10…1}
do
echo $WESTOS
done
3)in后使用seq连续选择命令定义变量值
即(seq 起始值 结束值),这一命令可以设置步长(seq 起始值 步长 结束值)
示例:
for WESTOS in $(seq 1 2 10)
do
echo $WESTOS
done
4)使用表达式方式定义变量值
即(单次表达式;条件表达式;末尾循环体)。for循环执行时,会先判断条件表达式是否成立,如果条件成立则执行中间循环体语句,执行完中间循环体后接着执行末尾循环体 ,在执行完末尾循环体后对条件表达式再次进行判断,若条件还成立,则继续重复中间循环体,当条件不成立时则跳出for循环。
示例:
for ((WESTOS=0;WESTOS<10;WESTOS++))
do
echo $WESTOS
done
练习:
1、编写脚本check_host.sh批量检测主机网络是否能够ping通
2、while…do条件语句
while语句作⽤是在条件为真执⾏动作,这一条件语句的结构为:
while ture
do 执⾏动作
done 结束标志
while true ##条件为真
do ##条件成立时做循环动作
done ##结束
实验:
#!/bin/bash
while true
do
read -p "Please input word: "EXIT
[ "$EXIT" = "exit" ] && exit
echo $EXIT
done
3、until…do条件语句
until语句作⽤是在条件为假执⾏动作,这一条件语句的结构为:
until false
do 执⾏动作
done 结束标志
until false ##条件为假
do ##条件不成立时做循环动作
done ##结束
实验:
until [ "$1" = "yes" ]
do
echo hello linux
exit
done
4、if …then…elif…then…else…fi 语句
if …then…elif…then…else…fi语句作⽤是多次判定条件执⾏动作, 这一语句可以将多个条件判断结合起来,其代码结构如下:
if ##首次判定
then ##条件成立执行动作
执⾏动作
elif ##当首次判定不成立时再次判定
then ##条件成立执行动作
执⾏动作
'....' ##elif可以多次书写
else ##所有的条件不成立执行动作
执⾏动作
fi 结束标志
练习:
要求:
check_file.sh
please input filename:file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
while true
do
read -p "please input filename:" FILENAME
if [ "$FILENAME" = "exit" -o "$FILENAME" = "EXIT" ]
then
echo bye
exit
elif [ ! -e "$FILENAME" ]
then
echo "文件不存在"
elif [ -d "$FILENAME" ]
then
echo "是个目录"
elif [ -S "$FILENAME" ]
then
echo "是个套结字"
elif [ -L "$FILENAME" ]
then
echo "是个软链接"
elif [ -f "$FILENAME" ]
then
echo "是个普通文件"
elif [ -b "$FILENAME" ]
then
echo "是个块设备"
elif [ -c "$FILENAME" ]
then
echo "是个字符设备"
fi
done
5、case语句
#!/bin/bash
case $1 in
westos) ##如果输入为westos
echo linux ##输入为linux
;;
linux) ##如果输入为linux
echo westos ##输出westos
;;
*) ##其余的情况
echo java
esac ##结束
脚本练习:
输入add,添加用户,输入del,删除用户
case $1 in
add|ADD)
read -p "please input username:" a
id $a > /dev/null &&{
echo “user is already exist!”
}||{
useradd $a
}
;;
del|DEL)
read -p "please input username:" d
id $d > /dev/null ||{
echo “user is not exist!”
}&&{
userdel $d
}
;;
*)
echo error
esac
6.expect(类似于shell,命令解释器)
expect看的是命令,而不是行数
脚本练习:
vim ask.sh
#!/bin/bash
read -p "Please input your name: " NAME
read -p "Please input your age: " AGE
read -p "Wich objective you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old study $OBJ FEEL $FEEL"
expect应答脚本:
dnf install expect -y
1.ask.sh(问题脚本)
#!/bin/bash
read -p "Please input your name: " NAME
read -p "Please input your age: " AGE
read -p "Wich objective you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old study $OBJ FEEL $FEEL"
2.answer.exp(回答脚本)
#!/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 }
"age" { send "$AGE\r"; exp_continue }
"objective" { send "$OBJ\r"; exp_continue }
"happy" { send "$FEEL\r" }
}
expect eof
shell应答脚本:
sh answer.exp lee 18 linux happy
#!/bin/bash
/usr/bin/expect <<EOF
set timeout 3
spawn /mnt/ask.sh
expect {
"name" { send "$1\r";exp_continue }
"age" { send "$2\r";exp_continue }
"objective" { send "$3\r";exp_continue }
"happy" { send "$4\r" }
}
expect eof
EOF
7.break,continue,exit
continue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出