Shell script for, while, case statement details and case

################for loop statement structure#############
When using for loop statement, you need Specify a variable and a list of possible values, and repeat the same command sequence for each different value, until the variable value runs out and exit the loop. Here, the value table is called the execution condition of the for statement, which includes multiple objects with the same attributes, which need to be specified in advance (such as address book, IP blacklist)
·
Syntax style
for variable name in value list
do
  command sequence
done
·
#####for loop statement 列子####
Requirement: According to the employee name given by the personnel department Pinyin list, add the corresponding user account in the Linux server,
the initial password is set to “123456”, among which the number of accounts in the employee list is not fixed and different.
·
1. Edit text
[[email Protected] ~]# cd /opt
[[email Protected] opt]# vi users.txt
zhang3
li4
wang5
sun6
2. Edit script
##Create multiple users
[[emailprotected] opt]# vi uaddfor.sh
#!/bin/bash
ULIST =$(cat /opt/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo “123456” | passwd –stdin $UNAME &>/dev/null< br>done
·
##Delete users added by uaddfor.sh script
cd opt
vi udelfor.sh
#!/bin/bash
ULIST=$(cat / opt/users.txt)
for UNAME in $ULIST
do
userdel -r $UNAME &>/dev/null
id $UNAME
done
·
# #IPAddress Check Host Status
[[emailprotected] opt]# vi ipadds.txt
192.168.10.1
192.168.10.2
192.168.10.3
192.168.10.4
·< br>[[email Protected] opt]# vi chkhosts.sh
#!/bin/bash
HLIST=$( cat /opt/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
[[emailprotected] opt]# chmod +x chkhosts.sh
[[emailprotected] opt ]# ./chkhosts.sh
host 192.168.32.1 is down.
host 192.168.32.2 is up.
host 192.168.32.3 is down.
host 192.168.32.11 is up.
·
#############while loop statement structure###########
for loop statement is very suitable for list objects without rules, and the list source is fixed For the occasion of the need to control the number of loops, the operation objects are numbered in numerical order, and repeated operations are performed according to specific conditions, it is more suitable to use a loop statement-the while statement.
The process of using the while statement: first judge the result of the conditional test operation after the while, if the condition is established, execute the do…done loop body; after returning to the while, judge the result of the conditional test again, if the condition is still established, then Continue to wrap the loop body; after returning to while again, the conditional test result is judged……..This loops until the conditional test result after the while is no longer established, and finally jumps to the done statement, which means the loop is over.
Whlie features: Repeated testing of a certain condition, as long as the condition is satisfied, repeated execution
·
Syntax style
while conditional test operation
do
Command sequence
done
·
#####while loop statement example#####
Add users in batches
User names start with stu and number them in numerical order.
A total of 20 users are added, namely stu1, stu2,……,stu20
The initial password is set to 123456
·
[[email protected] opt]# vi uaddwhile.sh
#!/bin/bash
PREFIX=”stu “
i=1
while [$i -le 20 ]
do
useradd ${PREFIX}$i
echo “123456” | passwd –stdin ${PREFIX}$i &> /dev/null
let i++
done
[[emailprotected] opt]# chmod +x uaddwhile.sh
[[emailprotected] opt]# ./uaddwhile.sh
·
###delete user###
[[emailprotected] opt]# cd /opt
[[emailprotected] opt]# vi udelwhile.sh
#!/bin/ bash
PERFIX=”stu”
i=1
while [$i -le 20 ]
do
userdel -r ${PERFIX}$i
let i++
done
[[emailprotected] opt]# chmod +x udelwhile.sh
[[emailprotected] opt]# ./udelwhile.sh
·
·
###Example 2 ###
Guess the commodity price game
Get random numbers through the variable RANDOM
Prompt the user to guess and record the number of times, and exit the loop after the guess is successful
·
[[emailprotected] opt]# vi pricegame.sh
#!/bin/bash
PRICE=$(expr $RANDOM% 1000)
TIMES= 0
echo “The actual price range of the product is 0-999, guess what it is”
while true
do
read -p “Please enter the number of prices you guessed:” INT
let TIMES++
if [$INT -eq $PRICE]; then
echo “Congratulations on your answer, the actual price is $PRICE”
echo “You guessed it correctly $TIMES times”
exit 0
elif [$INT -gt $PRICE]; then
echo “Too high! “
else
echo “Too low! “
fi
done
·
###Interpretation###
1, while true means an infinite loop
2, $RANDOM is an internal function of Bash, which is randomly selected The value range is between 0-32767
·
[[email Protected] opt]# chmod +x pricegame.sh
[[email Protected] opt]# ./pricegame.sh
Commodity’s The actual price range is 0-999, guess what it is.
Please enter the number of prices you guessed: 900
too high!
Please enter the number of prices you guessed: 800
too high That’s it!

Please enter the number of prices you guessed: 325
Congratulations on your answer, the actual price is 325
You guessed it 13 times in total
·
## ###########case branch statement structure#############
For different values ​​of variables, execute different command sequences respectively
·< br>###Syntax style
case variable value in
mode 1)
command sequence 1
;;
mode 2)
command sequence 2
;;
 ……
)
Default command sequence
esac
·
###case usage characteristics
·
The end of the case line must be the word “in”, Each pattern must end with “)”
Double semicolon”;;” indicates the end of the command sequence
In the pattern string, square brackets can be used to indicate a continuous range, such as “[0-9]” ; It can also be represented by the vertical bar symbol “|”, such as “A|B”
The last “
” indicates the default mode, and the is equivalent to a wildcard.
·
# ##Example 1
Keystroke type recognition
Prompt the user to enter a character
Judging that the character is a letter, number or other character
·
vi hitkey.sh
#!/ bin/bash
read -p “Please enter a character and confirm with Enter:” KEY
case “$KEY” in
[az] | [AZ])
echo “what you entered Are letters.”
;;
[0-9])
echo “You entered numbers.”
;;
)
echo “You entered Is a space, function key or other control characters.”
esac
·
[[email Protected] opt]# chmod +x hitkey.sh
[[emailprotected] opt]# ./hitkey.sh
Please enter a character and confirm with Enter: A
You entered a letter.
[[email Protected] opt]# ./hitkey.sh
Please enter a character and confirm with Enter: a
You entered letters.
[[emailprotected] opt]# ./ hitkey.sh
Please enter a character and confirm with Enter: 1
You entered a number.
[[emailprotected] opt]# ./hitkey.sh Please enter a character and press Enter Confirmation: What you entered is a space, function key or other control characters.

Leave a Comment

Your email address will not be published.