In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Structure of case statements
Execute different command sequences for different values of variables
case Variable Value in //variable value is $variable name pattern 1) command sequence 1;; //double semicolon ending statement pattern 2) command sequence 2;; //double semicolon ending statement...... *) Default command sequence esaccase statement execution flow
case statement application demo [root@localhost opt]# vim demo01.sh#!/ bin/bashread -p "Please enter the score (0-100) : " keycase $key in [8-9][0-9]| 100) echo "Your grades are excellent" ;; 7[0-9]) echo "Your grades are good" ;; 6[0-9]) echo "Your grades pass" ;; [0-9]|[1-5][0-9]) echo "Your grades are unqualified" ;; *) echo "The grade you entered is incorrect, please re-enter"esac[root@localhost opt]# source demo01.sh Please enter grade (0-100) : 88 Your grade is excellent [root@localhost opt]# source demo01.sh Please enter grade (0-100) : 76 Your grade is good [root@localhost opt]# source demo01.sh Please enter grade (0-100) : 66 Your score passed [root@localhost opt]# source demo01.sh Please enter your score (0-100) : 59 Your score failed [root@localhost opt]# source demo01.sh Please enter your score (0-100) : 120 The score you entered is incorrect, please re-enter the structure of the for statement
Read different variable values to execute the same set of commands one by one
for variable name in value do command sequence donefor statement execution flow
for statement application demonstration
Add users in batches
1. User names are stored in the users.txt file, one per line
2. The initial password is set to 123456
3. verification script
[root@localhost opt]# vim users.txtSimitaBoke[root@localhost opt]# vim users.sh#!/ bin/bashTMP=$(cat /root/users.txt)for USER in $TMPdo useradd $USER echo "123456" |passwd --stdin $USER &> /dev/nulldone[root@localhost opt]# source users.sh[root@localhost opt]# tail -2 /etc/passwdSimita:x:1030:1030::/home/Simita:/bin/bashBoke:x:1031::/home/Boke:/bin/bashwhile sentence structure
Repeatedly test a condition and repeat it as long as it holds
while Conditional test operation do command sequence donewhile statement execution flow
while statement application demonstration
Guess commodity prices
1. Random number obtained by variable RANDOM
2. Prompt users to guess and record the number of times, guess after exiting the loop
[root@localhost ~]# vim demo08.sh#!/ bin/bashPRICE=$(expr $RANDOM % 1000)TIMES=0 echo "Commodity prices range from 0-999, guess what? "while truedo read -p "Please enter your guess price:" INT let TIMES++if [ $INT -eq $PRICE ];then echo "You guessed it, the actual price is $PRICE" echo "You guessed $TIMES total" exit 0elif [ $INT -gt $PRICE ];then echo "is too high! "else echo "is too low! " fidone[root@localhost ~]# source demo08.sh Commodity prices range from 0-999, guess what? Please enter your guess price:500 is too high! Please enter your guess price:250 is too high! Please enter the number of your guess:200 is too high! Please enter the number of your guess:150 is too low! Please enter the number of your guess:180 is too high! Please enter the number of your guess:160 Too low! Please enter the number of your guess:170 is too high! Please enter the number of your guess:165 Too high! Please enter the number of price you guessed:162 You guessed correctly, the actual price is 162 You guessed a total of 9 times until statement structure
Repeat testing a condition, repeating it as long as the condition is not true
until conditional test operation do command sequence done
until statement application demonstration
Send online messages to specified users
1. If the user is not online (to log in to the system), try every 10 minutes until the user logs in to the system and then send the message
2. The username and message are passed to the script via location parameters
[root@localhost ~]# vim demo01.sh #!/ bin/bashusername=$1#Determine message format if [ $# -lt 1 ];then echo "Usage:`basename $0` []" exit 1fi#Determine if user exists if grep "^$username:" /etc/passwd > /dev/null ;then :else echo "User does not exist"fi#User is online, if not online contact once every five seconds until who| grep "$username" > /dev/null;do echo "User not online" sleep 5donemes=$*echo $mes | write $username[root@localhost ~]# chmod +x demo01.sh[root@localhost ~]# ./ demo01.sh czt After the specified user logs in, the terminal interface receives a message [czt@localhost ~]$ Message from root@localhost.localdomain on pts/0 at 15:09... cztEOF
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.