Shell process control statement

Source: Internet
Author: User
Like other advanced languages, Shell provides commands for controlling program processes, including condition branches and loop structures, you can use these commands to create a very complex program www.2cto.com. unlike traditional languages, Shell is used to specify the condition value instead of Boolean...
Like other advanced languages, Shell provides commands for controlling program processes, including condition branches and loop structures, you can use these commands to create a very complex program www.2cto.com. unlike traditional languages, Shell is used to specify a condition value instead of a Boolean operator, but a command and a string. Linux shell has a set of its own flow control statements, including the condition statement (if), the loop statement (for, while), the select statement (case) I, the shell condition statement (if usage) if statement structure [if/then/elif/else/fi] if condition test statement thenaction [elif condition actionelseaction] fi if the condition test statement is not clear, refer: linux shell logical operators and logical expressions explain shell commands, which can be separated by semicolons or line breaks. If you want to write multiple commands in one line, you can separate them using. For example: [root @ test8 test] # 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; condition tests include [[], [], and test. note: [[] is separated by spaces. II. loop statement (for, while, until usage): • for loop usage (for/do/done) syntax structure: 1.for... In statement for variable in seq string doactiondone description: as long as the seq string is separated by space characters, each time... When reading in, the read value is given to the preceding variable in order. Instance (testfor. sh ):#! /Bin/sh for I in $ (seq 10); do echo $ I; done; 2.for( (value assignment; condition; Operation statement) for (value assignment; condition; calculation statement) do action done; instance (testfor2.sh ):#! /Bin/sh for (I = 1; I <= 10; I ++); do echo $ I; done; • while loop (while/do/done) while statement structure while condition statement doactiondone; instance 1 :#! /Bin/shi = 10; while [[$ I-gt 5]; do echo $ I; (I --); done; instance 2: (read file content cyclically :)#! /Bin/sh while read line; do echo $ line; done </etc/hosts; until loop statement www.2cto.com syntax structure: until condition do actiondone meaning: until the condition is met, and then exit. Otherwise, execute action. instance (testuntil. sh ):#! /Bin/sha = 10; until [[$ a-lt 0]; doecho $ a; (a-); done; result: III. shell selection statement (case and select usage) • case Selection Statement use (case/esac) syntax structure case $ arg in pattern | sample) # arg in pattern or sample ;; pattern1) # arg in pattern1; *) # default; esac description: pattern1 is a regular expression. the following character can be used: * any string? Any character [abc] a, B, or c. any character [a-n] from a to n | multi-choice instance :#! /Bin/sh case $1 instart | begin) echo "start something"; stop | end) echo "stop something"; *) echo "Ignorant ";; esac running result: ============================== www.2cto.com testcase. sh startstart something • How to use the select statement (generate menu selection) syntax: select variable name in seq variable do action done instance :#! /Bin/sh select ch in "begin" "end" "exit" docase $ ch in "begin") echo "start something"; "end ") echo "stop something"; "exit") echo "exit" break; *) echo "Ignorant"; esacdone; running result: Note: select is a loop selection, it is generally used with case statements.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.