The linux shell has its own set of flow control statements, including conditional statements (if), loop statements (for, while), and select statements (case). Below I will introduce the use of each statement by example.
First, shell conditional statements (if usage)
if statement structure [if/then/elif/else/fi]
if conditional test statement
then
action
[elif condition
action
else
Action
]
fi
If it is not clear about the conditional test statement, you can refer to: linux shell logical operators, logical expressions
The shell command can be split by semicolon or by line break. If you want to write multiple commands in one line, you can split them by “';”.
如:
[chengmo@centos5 ~]$ a=5;if [[ a -gt 4 ]] ;then echo 'ok';fi; ok
Instance: (test.sh)
#!/bin/sh
scores=40;if [[ $scores -gt 90 ]]; then echo " Very good!";elif [[ $scores -gt 80 ]]; then echo "good!";elif [[ $scores -gt 60 ]]; then echo "pass!";else echo " No pass!";fi;
The conditional tests are: [[],[],test These, note: [[]] Variables are separated by spaces.
Second, loop statement (for, while, until use):
Syntax structure:
1.for … in statement for variable in seq string do action done Description: The seq string is separated by a space character. Each time for…in is read, the value will be read in order, giving the previous variable. Instances (testfor.sh): #!/bin/sh for i in $(seq 10); do echo $i;done; seq 10 produces 1 2 3 . . . . 10 space separated strings. 2.for((assignment; condition; operation statement)) for((assignment; condition; operation statement)) do action done; Instance (testfor2.sh): #!/bin/sh for((i=1;i<=10 ;i++));do echo $i;done; while statement structure while conditional statement do action done; Instance 1 : #!/bin/shi=10;while [[ $i -gt 5 ]];do echo $i; ((i--));done; Run results :================================================================================= :) #!/bin/sh while read line;do echo $line;done < /etc/hosts; Run Result: =================== sh testwhile2.sh # Do not remove the following line, or various programs# that Require network functionality will fail.127.0.0.1 centos5 localhost.localdomain localhost syntax structure: until condition do action done means: Exit until the condition is met. Otherwise execute action. Instance (testuntil.sh): #!/bin/sh a=10; until [[ $a -lt 0 ]];do echo $a; ((a—)); done; Results: sh testuntil.sh 109876543210 Three, shell selection statement (case, select usage) Syntax Structure case $arg in pattern
I believe that many users who use Linux systems will feel that command line operations are faster th
directory summary: /: root directory, generally only the directory under the root directory, do not
According to the bus structure, it can be divided into a bus type and a non-bus type. As we often
Executing programs are called processes. When a program is stored in an executable file and is runni
Explain the various problems that occur when Fedora 9 is installed.
Check whether the wireless network card supports creating hotspots under Linux
The three query methods for the last reboot time of the Linux system
Linux date command to view and set the time details (graphic)
IOCP, kqueue, epoll ... How important is it?
Linux system management skills big
Installation and configuration of Ethernet card under Linux operating system
Win10 window title bar height and scroll bar width how to set
What should I do if the printer prints text in the XP system?
What should I do if the win7 computer page has no frosted glass effect?
How to use the new alarm clock application in Win10 9926
Microsoft announced that Win10 "three most" enterprise version test users over 96%
What should I do if the troubleshooting tool in the Win10 control panel cannot be used?
Microsoft encourages companies to use IE10 browser