Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case and loop statements of Shell scripts

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Case and loop statement of Shell script this chapter structure: case multi-branch statement loop statement for loop statement while loop statement one. Case multi-branch statement

The case statement is mainly suitable for the following situations: there are multiple values for a variable, and a different command sequence needs to be executed for each of them. This situation is very similar to a multi-branch If statement, except that if determines several different conditions as needed, while case simply determines the different values of a variable.

1. Project actual combat

Check the type of characters entered by the user

Prompt the user to input a character from the keyboard, determine whether the character is a letter, number or other control character through the case statement, and give the corresponding prompt information

#! / bin/bashread-p "Please enter a character" keycase $key in [a Murz] | [Amurz]) echo "you entered a letter";; [0-9]) echo "you entered a number" *) echo "you entered a special symbol" esac~ [root@localhost ~] #. / 1.sh please enter a character a you enter the letter [root@localhost ~] #. / 1.sh please enter a character S you enter the letter [root@localhost ~] #. / 1.sh please enter a character 3 you enter a number [root @ localhost ~] #. / 1.sh Please enter a character & you are entering a special symbol II. For loop statement

When using the for loop statement, you need to 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 of the loop. Here, the "value list" is called the execution condition of the for statement, in which multiple objects with the same properties need to be specified in advance.

1. Project actual combat

Add user accounts in batches according to the name, and set the initial password "123456"

1.1.First, make a name list file [root@localhost ~] # vim / root/users.txtchenwenzhangsanwangwulisi~ 1.2 add users and set passwords in batch [root@localhost ~] # vim uaddfor.shroublesheet binder bindings $(cat / root/users.txt) for UNAME in $ULISTdo useradd $UNAME echo "123456" | passwd-- stdin $UNAME & > / dev/nulldone~ ~ 1.3 Test and execution results [root@localhost ~] # chmod + x uaddfor.sh [root@localhost ~] #. / uaddfor.sh [root@localhost ~] # tail-4 / etc/passwdchenwen:x: 1021:1021::/home/chenwen:/bin/bashzhangsan:x:1022:1022::/home/zhangsan:/bin/bashwangwu:x:1023:1023::/home/wangwu:/bin/bashlisi:x:1024:1024::/home/lisi:/bin/bash2. The second actual combat

A classmate checked his score.

#! / bin/bashread-p "Please enter your score (0-100)" scourecase $scoure in [8-9] [0-9] | 100) echo "excellent"; 7 [0-9]) echo "good";; 6 [0-9]) echo "qualified"; [0-9] | [1-5] [0-9]) echo "unqualified" *) incorrect input of "echo" Please re-enter "esac~ [root@localhost] #. / e.sh please enter your score (0100) 90 excellent [root@localhost] #. / e.sh please enter your score (0100) 75 good [root@localhost] #. / e.sh please enter your score (0100) 51 failed [root@localhost] #. / e.sh Please enter your score (0100123) incorrectly Please re-enter the three. While loop statement

When using a while loop statement, you can execute a sequence of commands repeatedly according to a specific condition until the condition is no longer satisfied. In the application of script

Endless loops should be avoided, otherwise subsequent command operations will not be executed. Therefore, the command sequence in the loop should include statements that modify the test condition, so that the test condition is no longer established at the appropriate time, thus ending the loop.

1. Actual combat project

Add 20 user accounts in batch, named stt1,stu2... stt20, and set the password 123456.

[root@localhost ~] # vim 3.sh

# / bin/bashPREFLX= "stt" i=1while [$I-le 20] do useradd ${PREFLX} $I echo "123456" | passwd-- stdin ${PREFLX} $I & > / dev/nulllet i++done [root@localhost ~] # chmod + x 3.sh [root@localhost ~] #. / 3.sh [root@localhost] # tail-3 / etc/passwdstt18:x:1042:1042::/home/stt18:/bin/bashstt19:x:1043:1043::/home/stt19:/bin / bashstt20:x:1044:1044::/home/stt20:/bin/bash2. The second actual combat project

Guess the price of the goods.

Requirement: generate a random number of prices (0mur999) as the actual price, judge whether the price guessed by the user is too high or too low, and output the number of guesses and the actual price until the user guesses the actual price.

[root@localhost ~] # vim 4. The price range of the product is 0,999, guess what? "(expr $RANDOM% 1000) TIMES=0echo." While truedo read-p "Please enter the number of prices you guessed:" INT let TIMES++if [$INT-eq $PRICE]; then echo "Congratulations. The actual price is $PRICE" echo "you guessed a total of $TIMES times" exit 0elif [$INT-gt $PRICE]; then echo "too high!" Else echo "too low!" The actual price range of fidone~ [root@localhost ~] # chmod + x 4.sh [root@localhost ~] #. / 4.sh goods is 0999. guess what it is? Please enter the price you guessed: 345 is too low! Please enter the price you guessed: 456 is too low! Please enter the price you guessed: 678 is too high! Please enter the price you guessed: 378 is too low! Please enter the price you guessed: 400 is too low! Please enter the price you guessed: 450 is too low! Please enter the price you guessed: 455 is too low! Please enter the price you guessed: 456 is too low! Please enter the price you guessed: 567 is too low! Please enter the price you guessed: 589 is too high! Please enter the price you guessed: 580 is too low! Please enter the number of prices you guessed: 581 Congratulations on your correct answer. The actual price is 581. If you have guessed more than 12 times, that's all we have.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report