1、for语句
作用:为循环执行动作
for 语句结构:
for 定义变量
do 使用变量,执行动作
done 结束标志
for语句的基本格式
格式1:
#!/bin/bash
for WESTOS in $(seq 1 2 10)`
do
echo $WESTOS
done
[root@westos111 mnt]# cat num.sh
#!/bin/bash
for NUM in `seq 1 2 10` ##输出1到10的中的奇数
do
echo -n $NUM
sleep 1
echo -ne "\r"
done
格式2:
for WESTOS in westos linux lee
do
echo $WESTOS
done
格式3:
for WESTOS in {10..1}
do
echo $WESTOS
done
[root@westos111 mnt]# cat num.sh
#!/bin/bash
for NUM in 1 2 3 4
do
echo -n $NUM
[ "$NUM" = "4" ] && exit
sleep 1
echo -ne "\r" ##依次输出变量
done
[root@westos111 mnt]# cat num.sh
#!/bin/bash
for NUM in {1..10}
do
echo -n $NUM
[ "$NUM" = "10" ] && exit
sleep 1
echo -ne "\r"
done
格式4:
for ((WESTOS=0;WESTOS<10;WESTOS++))
do
echo $WESTOS
done
[root@westos111 mnt]# cat num.sh
#!/bin/bash
for ((NUM=0;NUM<=10;NUM++))
do
echo -n $NUM
sleep 1
echo -ne "\r" ##-n不换行输出 -e处理特殊字符 \r将光标移动至首行但不换行
done
脚本练习:
1、check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表
[root@westos111 mnt]# cat check_host.sh
#!/bin/bash
for I in 172.25.254.{1..10}
do
ping -c1 -w1 $I &> /dev/null && {
echo $I
}
done
2、建立文件userlist 和passlist ,创建脚本执行sh user_create.sh userlist passlist时,建立userlist中的用户,密码在文件passlist中
[root@westos111 mnt]# cat user_create.sh
#!/bin/bash
[ -z "$1" ] && {
echo error:please input list
exit
}
[ -z "$2" ] && {
echo error:please input two lists
exit
}
Line_Num=`sed -n $= $1`
for LINE in `seq 1 $Line_Num`
do
USERNAME=`sed -n ${LINE}p $1`
PASSWORD=`sed -n ${LINE}p $2`
id $USERNAME &> /dev/null && {
echo $USERNAME is exist
}||{
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && {
echo $USERNAME is created
}
}
done
2、条件语句
while....do 语句
作用:条件为真执行动作
语句结构:
while ture <!--条件为真-->
do <!--条件成立所作循环动作-->
done <!--结束-->
until...do 语句
作用:条件为假执行动作
语句结构:
until false <!--条件为假-->
do <!--条件不成立所作循环动作-->
done <!--结束-->
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为止
[root@westos111 mnt]# cat check_file.sh
#!/bin/bash
while true
do
read -p "please input filename: " FILE
if [ "$FILE" = "exit" ]
then
exit
elif [ ! -e "$FILE" ]
then
echo $FILE is not exist
elif [ -L "$FILE" ]
then
echo $FILE is link file
elif [ -f "$FILE" ]
then
echo $FILE is common file
elif [ -d "$FILE" ]
then
echo $FILE is directory
fi
done
3、case
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
脚本练习:创建、删除用户
create_user.sh add
输入用户名,如果存在显示用户已存在并继续输入用户名,如果不存在,显示输入密码,创建用户输入exit退出。
create_user.sh del
输入用户名,如果存在则删除用户。如果不存在则显示用户不存在并再次要求输入用户名。
[root@westos137 mnt]# cat create_user.sh
#!/bin/bash
USERCMD()
{
read -p "Please input username : " USERNAME
if [ "$USERNAME" = "exit" ]
then
exit
fi
}
case $* in
add|ADD|Add)
while true
do
USERCMD
id $USERNAME &> /dev/null && {
echo "$USERNAME is exist"
}||{
read -p "Please input password : " PASSWD
useradd $USERNAME
echo $PASSWD|passwd --stdin &> /dev/null $USERNAME
echo $USERNAME is create
}
done
;;
del|DEL|Del)
while true
do
USERCMD
id $USERNAME &> /dev/null && {
userdel -r $USERNAME
echo "$USERNAME is deleted"
}||{
echo $USERNAME is not exist
}
done
;;
*)
echo "please input add|del following $0"
esac
4、expect
问题脚本:
[root@westos137 mnt]# cat ask.sh
#!/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
应答脚本:
[root@westos137 mnt]# cat ask.sh
#!/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
脚本练习:检测172.25.254.1-172.25.254.10网络是否开启
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
[root@westos137 mnt]# cat host_list.sh
#!/bin/bash
AUTO_SSH()
{
/usr/bin/expect <<EOF
spawn ssh -l root $1 hostname
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "westos\r"}
}
expect eof
EOF
}
if [ -e "hosts_list" ]
then
echo hosts_list is exist
else
for HOST in {1..10}
do
ping -c1 -w1 172.25.254.$HOST &> /dev/null && {
HOST_NAME=`AUTO_SSH 172.25.254.$HOST | tail -n 1`
echo -e " 172.25.254.$HOST\t$HOST_NAME" >> host_list
}
done
fi
5、break,continue,exit
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出