Linux Shell Process Control (condition if, loop [For, while], select [case] statement instance

Source: Internet
Author: User

1. Shell Condition Statement (if usage)

If statement structure [if/then/Elif/else/FI]

If condition test statement

Then

Action

[Elif Condition

Action

Else

Action

]

Fi

If you are not clear about the conditional test statement, refer to: Linux Shell logical operators and logical expressions.

Shell commands 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:

[Chengmo @ centos5 ~] $ A = 5; if [[A-GT 4]; Then ECHO 'OK'; FI;
OK

Example: (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;


Conditional tests include [[], [], and test. Note: [[] and variables are separated by spaces.

2. Loop statements (for, while, until usage ):

  • For Loop usage (for/do/done)

Syntax structure:

 1.... In statement

For variable in seq string

Do

Action

Done

Note: 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;


SEQ 10 generates 1 2 3 .... A string separated by 10 spaces.

2. For (assign value; condition; Operation statement ))

For (assign value; condition; Operation statement ))

Do

Action

Done;

Instance (testfor2.sh ):

#! /Bin/sh

For (I = 1; I <= 10; I ++); do
Echo $ I;
Done;


  • While loop usage (while/do/done)

While statement Structure

While Condition Statement

Do

Action

Done;

Instance 1:

#! /Bin/sh
I = 10;
While [[$ I-GT 5]; do
Echo $ I;
(I --));
Done;

Running result: ==============================

Sh testwhile1.sh
10
9
8
7
6

Example 2: (Read File Content cyclically :)

#! /Bin/sh

While read line; do
Echo $ line;
Done </etc/hosts;

 

Running 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

  • Until loop statement

Syntax structure:

Until Condition

Do

Action

Done

It means to exit after the condition is met. Otherwise, execute action.

Instance (testuntil. Sh ):

#! /Bin/sh

A = 10;

Until [[$ A-lt 0]; do

Echo $;

(-));

Done;

Result:

Sh testuntil. Sh

10
9
8
7
6
5
4
3
2
1
0

3. Shell selection statement (case and select usage)

  • Use Case/esac)

Syntax structure

Case $ ARG in
Pattern | sample) # ARG in pattern or sample
;;
Pattern1) # ARG in pattern1
;;
*) # Default
;;
Esac

Note: pattern1 is a regular expression, which can contain the following characters:

* Any string
? Any character
[ABC] A, B, or C
[A-N] any character from A to N
| Multiple options


Instance:

#! /Bin/sh

Case $1 in
Start | begin)
Echo "start something"
;;
Stop | end)
Echo "stop something"
;;
*)
Echo "ignorant"
;;
Esac

Running result: ===============================


Testcase. Sh start
Start 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"
Do
Case $ ch in
"Begin ")
Echo "start something"
;;
"End ")
Echo "stop something"
;;
"Exit ")
Echo "exit"
Break;
;;
*)
Echo "ignorant"
;;
Esac
Done;

Running result:


Note: select is a loop selection, which 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.