一.for语句
作用:为循环执行动作
for语句结构:for 定义变量
do 使用变量,执行动作
done 结束标志
1.for语句的基本格式
格式1:
#!/bin/bash
for WESTOS in $(seq 1 2 10)
do
echo $WESTOS
done
显示1-10中的奇数:
#!/bin/bash
for NUM in `seq 1 2 10`
do
echo -n $NUM ##-n不换行输出
[ "$NUM" = "10" ] && exit
sleep 1
echo -ne "\r" ##-e 处理特殊字符, \r 将光标移动至首行,但不换行
done
格式2:
#!/bin/bash
for WESTOS in westos linux lee
do
echo $WESTOS
done
[root@westoslinux mnt]# vim test1.sh
#!/bin/bash
for NUM in 1 2 3 4
do
echo -n $NUM ##-n表示不换行输出
[ "$NUM" = "4" ] && exit ##当显示4的时候退出
sleep 1
done
#!/bin/bash
for NUM in 1 2 3 4
do
echo -n $NUM
[ "$NUM" = "4" ] && exit
sleep 1
echo -ne "\r" ##-e 处理特殊字符, \r 将光标移动至首行,但不换行
done
格式3:
#!/bin/bash
for WESTOS in {10..1}
do
echo $WESTOS
done
#!/bin/bash
for NUM in {1..10}
do
echo -n $NUM
[ "$NUM" = "10" ] && exit
sleep 1
echo -ne "\r" ##-e 处理特殊字符, \r 将光标移动至首行,但不换行
done
格式4:
#!/bin/bash
for ((WESTOS=0;WESTOS<10;WESTOS++ ))
do
echo $WESTOS
done
vim test1.sh ##显示数字从0到10,每隔一秒钟显示一次并清空之前的显示
#!/bin/bash
clear
for ((NUM=0;NUM<=10;NUM++))
do
echo -n $NUM
sleep 1
echo -ne "\r"
done
脚本练习1:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通常请显示主机的ip列表
[root@westoslinux mnt]# vim check_host.sh
#!/bin/bash
for HOST in 172.25.254.{1..10}
do
ping -c1 -w1 $HOST &> /dev/null &&{
echo $HOST
}
done
[root@westoslinux mnt]# sh check_host.sh
172.25.254.10
脚本练习2:
[root@westoslinux mnt]# vim user_create.sh
#!/bin/bash
[ -z "$*" ] &&{
echo "Please input userlist!!" ##判断脚本之后是否为空
exit
}
[ ! -e "$*" ] &&{
echo "$* is not exist!!" ##判断脚本之后跟得文件是否存在
exit
}
for USER in `cat $*`
do
id $USER &> /dev/null &&{ ##userlist文件中的用户如果在系统中存在就不建立,如果不存在建立用户
echo "$USER is exist"
}||{
useradd $USER
echo "$USER is created!!"
}
done
脚本练习3:
[root@westoslinux mnt]# vim user_create.sh
#!/bin/bash
[ -z "$1" ] &&{
echo "Please input userlist!!"
exit
}
[ -z "$2" ] &&{
echo "Please input passwdfile!!"
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 exit
}||{
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && {
echo $USERNAME is created!!
}
}
done
二.条件语句
while...do语句
作用:条件为真执行动作
语句结构:
while ture <!--条件为真-->
do <!--条件成立所作循环动作-->
done <!--结束-->
[root@westoslinux mnt]# vim test3.sh
#!/bin/bash
for i in {1..5}
do
while [ "$i" = "5" ]
do
read -p "Please input word:" WORD
echo $WORD
done
echo $i
done
until...do 语句
作用:条件为假执行动作
语句结构:
until false <!--条件为假-->
do <!--条件不成立所作循环动作-->
done <!--结束-->
[root@westoslinux mnt]# vim test3.sh
#!/bin/bash
for i in {1..5}
do
until [ "$i" != "5" ]
do
read -p "Please input word:" WORD
echo $WORD
done
echo $i
done
if...then...elif...then...else...fi 语句
作用:多次判定条件执行动作
代码结构:
if <!--首次判断定-->
then <!--条件成立执行动作-->
elif <!--当首次判定不成立时再次判定-->
then <!--条件成立执行动作-->
... <!--elif可以书写多次-->
else <!--所有条件不成立执行动作-->
fi <!--结束-->
[root@westoslinux mnt]# vim test3.sh
#!/bin/bash
while true
do
read -p "Please input filename for check:" 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
脚本练习
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
[root@westos139 mnt]# vim check_file.sh
#!/bin/bash
while true
do
read -p "Please input filemane for check:" FILE
if [ "$FILE" = "exit" ]
then
echo bye
exit
elif [ ! -e "$FILE" ]
then
echo "$FILE is not exist"
elif [ -d "$FILE" ]
then
echo "$FILE is a directory"
elif [ -f "$FILE" ]
then
echo "$FILE is a regular file"
elif [ -S "$FILE" ]
then
echo "$FILE is socket"
fi
done
三.case
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
[root@westos139 mnt]# vim case.sh
#!/bin/bash
case $1 in
westos|WESTOS)
echo linux
;;
linux|LINUX)
echo westos
;;
*)
echo error
esac
脚本练习:
create_user.sh add
输入用户名,如果存在显示用户已存在并继续输入用户名,如果不存在,显示输入密码,创建用户输入exit退出。
create_user.sh del
输入用户名,如果存在则删除用户。如果不存在则显示用户不存在并再次要求输入用户名。
[root@westos139 mnt]# vim 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: " -s PASSWORD
useradd $USERNAME
echo $PASSWORD|passwd --stdin $USERNAME &> /dev/null &&{
echo ""
echo "$USERNAME is created"
}
}
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 for following $0"
esac
四.expect
[root@westos139 mnt]# vim ask.sh
#!/bin/bash
read -p "what's your name: " NAME
read -p "HOw old are you: " AGE
read -p "Which objective you study: " OBJ
read -p "Are you happy?" FEEL
echo $NAME is $AGE\'s old study $OBJ feel $FEEL
[root@westos139 mnt]# vim answer.sh
/mnt/ask.sh <<EOF
gyy
20
linux
happy
EOF
[root@westos139 mnt]# dnf install expect -y
[root@westos139 mnt]# vim ask.sh ##问题脚本
#!/bin/bash
read -p "what's your name: " NAME
read -p "HOw old are you: " AGE
read -p "Which objective you study: " OBJ
read -p "Are you happy?" FEEL
echo $NAME is $AGE\'s old study $OBJ feel $FEEL
[root@westos139 mnt]# vim answer.exp ##应答脚本
#!/bin/expect
set timeout 3
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 }
“happy” { send "$FEEL\r";exp_continue }
}
expect eof
[root@westos139 mnt]# vim answer.sh
/usr/bin/expect <<EOF
spawn /mnt/ask.sh
expect {
"name" { send "$1\r";exp_continue }
"old" { send "$2\r";exp_continue }
"objective" { send "$3\r";exp_continue }
"happy" { send "$4\r"}
}
expect eof
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
[root@westos139 mnt]# vim 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 "host_list" ]
then
echo host_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
五.break,continue,exit
continue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出
[root@westos139 mnt]# vim westos.sh
#!/bin/bash
for NUM in {1..10}
do
if [ "$NUM" = "5" ]
then
echo luck number
continue
fi
echo $NUM
done
echo end