当前位置:网站首页>Shell (7) case statement
Shell (7) case statement
2022-07-27 01:43:00 【A simple comparison】
List of articles
Shell(7)case sentence
Preface
In a script , We often need to interact , Execute different commands according to the results of interactive input , And that's where it comes in case sentence .
And if The difference is ,case The statement is more like a menu , You don't need to make your own judgment , According to different results preset in advance , Execute the corresponding command .
One 、read command
read Commands are the main instructions we use to interact in scripts , He can define variables according to the way of interactive input .read yes Shell Built in commands , If it doesn't work Redirect , The default is to read the data entered by the user from the keyboard ; If redirected , Then you can read data from the file .read The basic format of the command is :
read [ Options ] [ Variable name ] # Variable names can be multiple
Options :
| Options | Content |
|---|---|
| -p | Display prompt message , And do not use line breaks , The tip is prompt. |
| -n | Read at most n Stop after characters , Stop immediately if you encounter a carriage return or line change halfway . |
| -N | Read at most n Stop after characters , If you encounter a carriage return or line change, it will not stop . |
| -r | Read as is (Raw mode), Do not interpret backslash characters as escape characters . |
| -s | silent mode (Silent mode), The entered characters will not be displayed on the screen . When entering passwords and other confirmation information , It is necessary to . |
| -e | When getting user input , Code conversion of function keys , The character corresponding to the function key will not be displayed directly . |
| -d | Use string delimiter Specify the position where the reading ends , Instead of a newline ( The data read does not include delimiter). |
| -a | Assign the read data to the array array, From the subscript 0 Start . |
| -u | Use file descriptors fd As an input source , Instead of standard input , Similar to redirection . |
| -t | Set timeout , If the input is not finished after this time ,read No data is stored . |
example : Enter your name interactively , And output this variable :

Two 、case sentence
case Statement can make the structure of the script program clearer 、 distinct , Commonly used for service startup 、 restart 、 Stopped script , Some services do not provide such control scripts , Need to use case Statement writing .

case Statement is mainly applicable to the following situations : There are many values for a variable , You need to execute different command sequences for each value . This is the case with multi - branched if The sentences are very similar , It's just if Statements need to judge multiple different conditions , and case The statement just judges the different values of a variable .
case Grammatical structure :
case $ Variable in #case Line ending must be a word “in”
Pattern 1) # The first mock exam must be the right bracket “)” end .
Command sequence 1
;; # Double a semicolon “;;” Indicates the end of the command sequence .
Pattern 2) # In pattern string , You can use square brackets to indicate a continuous range , Such as “[0-9]” exceed 9 The number of requires a regular expression ; You can also use the vertical bar symbol “|” Represents or , Such as “A|B”.
Command sequence 2
;;
* ) # final “*)” Represents the default mode , Among them * It's equivalent to a wildcard .
Command sequence 3 # There is no need to add a double semicolon after the last mode .
esac # hold case Write backwards , End of the said case sentence
example : Enter a character , Determine the type of character :

END
边栏推荐
猜你喜欢
随机推荐
iptables
SSH and NFS services
FaceNet
DevEco-Could not resolve com.huawei.ohos:hap:2.4.5.0.错误
LAMP.
Web Service (04) -- Introduction and construction of lamp +discuz Forum
Summary of heap sorting related knowledge
ONEFLOW source code list: GDB compilation and debugging
[SQL injection] joint query
Definition of array
Typescript 14 starting from 0: built in tool type
[ctf real question] 2018 WANGDING cup web unfinish
1101: numbers in reverse order (function topic)
【多态】多态的详细介绍,简单易懂
Web services (07) - LNMP one click deployment
Shell (9) function
33三剑客awk
Network foundation of software test interview questions
【无标题】
Longest substring without duplicate characters




![[SQL injection] extended injection method](/img/a1/d4218281bfc83a080970d8591cc6d4.png)


![[by pass] bypass method of WAF](/img/dd/7204b2401a9f18c02c8b9897258905.png)

