Linux Shell Process Control

Source: Internet
Author: User

Linux Shell Process Control
Guide Unlike the Java, PHP, and other languages, the Linux shell has its own process control statements, including conditional statements (IF), loop statements (for,while), and SELECT statements (case). I'll use the example below to illustrate how each statement is used.

One, Shell conditional statements (if usage)

If statement structure [IF/THEN/ELIF/ELSE/FI]

If condition test statement thenaction[elif condition ACTIONELSEACTION]FI

Shell commands, which can be split by semicolons, or by line breaks. If you want to write multiple commands in one line, you can pass the ";" segmentation, such as:

[[email protected] ~]$ a=5;if [[A-GT 4]]; then echo ' OK '; fi;

Example: (test.sh)

#!/bin/shscores=40;if [[$scores-GT 90]]; Thenecho "Very good!"; elif [[$scores-GT 80]]; Thenecho "good!"; elif [[$scores-GT 60]]; Thenecho "pass!"; Elseecho "No pass!"; Fi

second, the circular statement (For,while,until usage):

(1) For loop use Method (For/do/done)
1.for ... in statement--syntax structure

The for variable in seq string       # seq string is separated by a space character, and each time for...in reads, the                                                              value is read sequentially to the preceding variable. Doactiondone

Example (testfor.sh):

#!/bin/shfor i in $ (seq 10); Do     #seq 10 generate 1 2 3 ... 10 space-delimited string echo $i;d one;

2.for (assignment; condition; op-statement))

for (assignment; condition; Operation statement)) Doactiondone;

Example (testfor2.sh):

#!/bin/shfor ((i=1;i<=10;i++));d Oecho $i;d one;

(2) While loop use (While/do/done)

While conditional statement doactiondone;

Example 1:

#!/bin/shi=10;while [[$i-GT 5]];d oecho $i;((i--));d one;

Operation Result:

SH testwhile1.sh109876

Example 2: (Loop read file contents:)

#!/bin/shwhile Read Line;doecho $line;d one </etc/hosts;

Operation Result:

SH testwhile2.sh# don't remove the following line, or various programs# that require network functionality would fail.127. 0.0.1 centos5 localhost.localdomain localhost

(3) Until loop statement--Grammatical structure

Until condition             #直到满足条件, exit. Otherwise, the action is executed. Doactiondone

Example (testuntil.sh):

#!/bin/sha=10;until [[$a-lt 0]];d oecho $a;((a));d one;

Results:

SH testuntil.sh109876543210
Iii. Shell Selection statement (case, select usage)

(1) Case Selection statement use (CASE/ESAC)--syntax structure

Case $arg Inpattern | Sample) # arg in pattern or sample;; PATTERN1) # arg in PATTERN1;; *) #default;; Esac

Description: Pattern1 is a regular expression and can be used with the following characters:
* Any string? Any character
[ABC] A, B, or C three characters one of them
[A-n] Any character from A to n
| Multiple options

Instance:

#!/bin/shcase $ Instart | Begin) echo "Start something";; Stop | End) echo "Stop something";; *) echo "ignorant";; Esac

Operation Result:

Testcase.sh StartStart Something

(2) SELECT statement Use method (Generate menu selection)--syntax

Select variable name in seq variable doactiondone

Instance:

#!/bin/shselect ch in "Begin" "End" "Exit" Docase $ch "Begin") echo "Start Something";; " End ") echo" stop Something ";;" Exit ") echo" exit "break;;; *) echo "ignorant";; Esacdone;

Operation Result:

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

Linux Shell Process Control

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.