case multi-branch statement
Structure case statements
For different values of variables, performs different command sequences
Example 1
Type identification keystroke
It prompts the user to enter a character
It determined that the characters are letters, numbers or other characters
#!/bin/bash
read -p "请输入一个字符:" key
case $key in
[a-z] | [A-Z])
echo "你输入的是字母"
;;
[0-9])
echo "你输入的是数字"
;;
*)
echo "你输入的是特殊字符"
esac
Example 2
Enter the results, view grades
#!/bin/bash
read -p "请输入你的成绩:" num
case $num in
[8-9][0-9]|100)
echo "优秀"
;;
7[0-9])
echo "良好"
;;
6[0-9])
echo "合格"
;;
[0-9]|[1-5][0-9])
echo "不合格"
;;
*)
echo "您输入有误"
esac
Structure for statements
Different read variable value, to perform the same set of commands one by one
Example 1
Add users in bulk
The user name is stored in name.txt file, one per line
The initial passwords are set to 123456
Validation script
#!/bin/bash
TMP=$(cat /root/name.txt)
for USER in $TMP
do
echo "用户是$USER"
useradd $USER
echo "123456" | passwd --stdin $USER > /dev/null
done
Example 2
The IP host status check geological
IP geological storage in demo04.txt file, one per line
Ping command to check connectivity of each host
#!/bin/bash
IP=$(cat /root/demo04.txt)
for ip in $IP
do
ping -c 1 -s 1 -w 3 $ip > /dev/null
if [ $? -eq 0 ]
then
echo "$ip主机存在"
else
echo "$ip主机不存在"
fi
done
Structure while statement
Repeat the test a condition, as long as the conditions are met repeatedly executed
Example 1
Add users in bulk
User name begins with stu, numbered in numerical order
Add a total of five users, that stu1, stu2, stu3 ...
The initial passwords are set to 123456
#!/bin/bash
num=1
while [ $num -le 5 ]
do
useradd stu$num
echo "123456" | passwd --stdin stu$num &> /dev/null
let num++
done
Example 2
Guess commodity prices script
RANDOM random number obtained by variable
And prompts the user to guess the number of records, after exiting the loop guess
#!/bin/bash
random=$(expr $RANDOM % 100)
tim=0
while true
do
read -p "请输入商品的价格:" jia
let tim++
if [ $jia -eq $random ]
then
echo "恭喜您猜对了"
echo "您一共猜了$tim"
exit 0
elif [ $jia -lt $random ]
then
echo "您猜的数小了"
else
echo "您猜的数大了"
fi
done
Integrated example
Enter five stores to buy goods were the last to see total spending
#!/bin/bash
i=1
sum=0
while [ $i -le 5 ]
do
echo "进入第$i家商店"
read -p "是否进入看看(yes/no)" doing
while [ $doing = "yes" ]
do
echo "1:衣服¥200"
echo "2:鞋子¥100"
echo "3:手套¥75"
echo "4:裤子¥150"
read -p "请选择需要购买的商品序列:" num
case $num in
1)
echo "衣服购买成功"
expr $[sum+=200] &> /dev/null
;;
2)
echo "鞋子购买成功"
expr $[sum+=100] &> /dev/null
;;
3)
echo "手套购买成功"
expr $[sum+=75] &> /dev/null
;;
*)
echo "裤子购买成功"
expr $[sum+=150 &> /dev/null
esac
read -p "是否继续进行购买(yes/no)" doing
done
let i++
if [ $doing = "no" ]
then
continue
fi
done
echo "购物总价:$sum"
Structure until the statement
Repeat the test a condition, as long as the condition is not satisfied then repeatedly executed
Example 1
And calculating the value 1-50
And calculate accumulated values of 1 to 50 by way of circulating
#!/bin/bash
sum=0
i=0
until [ $i -eq 51 ]
do
let sum+=$i
let i++
done
echo "总数之和为:$sum"
Example 2
Send online messages to the specified user
If the user is not online (not landing system), then try again every 5s, sending the message until after the user logs
Message to the user until the parameter script
#!/bin/bash
if [ $# -lt 0 ]
then
echo "Usage:$0 <user><message>"
exit 1
fi
grep "$1" /etc/passwd &> /dev/null
if [ $? -eq 0 ];then :
else
echo "用户不存在"
fi
until who|grep "$1" > /dev/null
do
echo "用户不在线"
sleep 5
done
echo $2 | write $1
thanks for reading! ! !