The shell, as a scripting language, also contains other program control structures such as loops, branches, and so on, making it easier to perform more complex and powerful functions.
Using the FOR Loop statement
At work, you often encounter a task that needs to be executed multiple times, and each execution is just a different object, and the other commands are the same. Using simple if statements is already difficult to meet, writing all the code will be difficult, and for loop statements will solve similar problems very well
1. Structure of the FOR statement
When using a For loop statement, you need to specify a variable and a list of possible values, repeat the same command for each different value, until the value of the variable exits the loop
Case 1: Build users in batches by name list
[[email protected] ~]# vim /root/users.txt
yang
shu
fan
[[email protected] ~]# vim uaddfor.sh
#!/bin/bash
ULIST=$(cat /root/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo "123456" | passwd --stdin $UNAME
done
[[email protected] ~]# sh uaddfor.sh
[[email protected] ~]# tail -3 /etc/passwd
yang:x:1011:1011::/home/chenye:/bin/bash
shu:x:1012:1012::/home/dengchao:/bin/bash
fan:x:1013:1013::/home/zhangjie:/bin/bash
Case 2: Detecting host status based on IP address list
[[email protected] ~]# vim /root/ipadds.txt
192.168.1.10
192.168.1.11
192.168.1.12
[[email protected] ~]# vim chkhosts.sh
#!/bin/bash
HLIST=$(cat /root/ipadds.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is up."
else
echo "Host $IP is down."
fi
done
[[email protected] ~]# sh chkhosts.sh
Host 192.168.1.10 is up.
Host 192.168.1.11 is down.
Host 192.168.1.12 is up.
Using the While Loop statement
The For statement applies to a list object that has no regularity, and the list is sourced in a fixed situation. It is more suitable for the while loop when it is required to control the number of cycles, to number the operands numerically, and to repeat operations on specific conditions.
1. The structure of the while statement
When using a while loop statement, you can repeatedly execute a sequence of commands based on a specific condition until the condition is not satisfied
Case 1: Adding a regular numbered user ("Let i++" is equivalent to "i= ' expr $i + 1 '")
[[email protected] ~]# vim uaddwhile.sh
#!/bin/bash
PREFIX="ysf"
i=1
while [ $i -le 20 ]
do
useradd ${PREFIX}$i
echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null
let i++
done
[[email protected] ~]# sh uaddwhile.sh
[[email protected] ~]# grep "ysf" /etc/passwd | tail -3
ysf18:x:1028:1028::/home/stu18:/bin/bash
ysf19:x:1029:1029::/home/stu19:/bin/bash
ysf20:x:1030:1030::/home/stu20:/bin/bash
Case 2: Guess the price game
The case requirements are as follows: The script randomly generates a price number (1-999) as the actual price, judging whether the user gives the correct price, give the corresponding hint after guessing again. Until the price is guessed, the number of times the user guesses, the actual price
Design idea: Obtain a 16-random integer less than 2 by environment variable, and calculate the remainder of 1000 to get the stochastic price of 0-999; Repeated guesses can be implemented by true as a while loop for test conditions
[[email protected] ~] # vim game.sh
#! / bin / bash
PRICE = $ (expr $ RANDOM% 1000)
TIMES = 0
echo "The actual product price is between 0-999, guess what?"
while true
do
read -p "Please enter the number of prices you guessed:" INT
let TIMES ++
if [$ INT -eq $ PRICE]; then
echo "Congratulations, you got it right, the actual price is $ PRICE"
echo "You guessed $ TIMES times in total"
exit 0
elif [$ INT -gt $ PRICE]; then
echo "It's too high!"
else
echo "It's too low!"
fi
done
[[email protected] ~] # sh pricegame.sh
The actual product price is between 0-999, guess what?
Please enter your guessed price: 500
Too high!
Please enter your guessed price: 250
too low!
Please enter your guessed price: 375
Too high!
Please enter your guessed price: 280
Too high!
Please enter your guessed price: 265
Too high!
Please enter your guessed price: 253
Congratulations, you got it right. The actual price is 253.
You guessed 6 times in total
Using the Case Branch statement
Case statements are mainly used when there are multiple values for a variable, and you need to execute a different sequence of commands for each of these values. Acquaintance with multi-branch if statements, only if statements need to judge many different conditions, while case simply determines the different values of a variable
1. Structure of Case statements
Case 1: Detecting character types entered by the user
[[email protected] ~] # cat hitkey.sh
#! / bin / bash
read -p "Please enter a character and press Enter to confirm:" KEY
case "$ KEY" in
[a-z] | [A-Z])
echo "You entered letters."
;;
[0-9])
echo "You entered a number."
;;
*)
echo "You entered a space, function key, or other control character."
esac
[[email protected] ~] # sh hitkey.sh
Please enter a character and press Enter to confirm: k
You entered the letter k.
[[email protected] ~] # sh hitkey.sh
Please enter a character and press Enter to confirm: 8
You entered the number 8.
[[email protected] ~] # sh hitkey.sh
Please enter a character and press Enter to confirm: [19 ~
You entered a space, function key, or other control character.
Case 2: Writing system service scripts (test)
[[email protected] ~] # vim /etc/init.d/myprog
#! / bin / bash
# chkconfig:-90 10
# description: Startup script for sleep Server
case "$ 1" in
start)
... starting XX service
;;
stop)
... stopping the XX service
;;
restart)
$ 0 stop
$ 0 start
;;
*)
echo "Usage: $ 0 {start | stop | restart}"
esac
[[email protected] ~] # chkconfig --add myprog
[[email protected] ~] # chkconfig --list myprog
myprog 0: close 1: close 2: close 3: close 4: close 5: close 6: close
[[email protected] ~] # /etc/init.d/myprog start
Starting sleep service ... OK
[[email protected] ~] # /etc/init.d/myprog stop
Stopping sleep service ... OK
[[email protected] ~] # /etc/init.d/myprog reload
Usage: ./myprog {start | stop | restart}
[[email protected] ~] # /etc/init.d/myprog restart
Stopping sleep service ... OK
Starting sleep service ... OK
Shell script for, while Loop statement and Case Branch statement