Linux shell Process Control (condition if, loop for, while)

Source: Internet
Author: User

Linux shell has a set of its own process control statements, including conditional statements (if), cyclic statements (for, while), and select statements (case ). The following example describes how to use each statement.

I. 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 the condition test statement is not clear, refer to: linux shell logical operators and logical expressions, or refer to the following parameters.

I. logical operators


Logical volume label 1. Detection of files and directories! -F is commonly used! Checks whether "file" exists. For example: if [-f filename]-d! Detect "directory"?-B: detect whether it is a "block file"-c: detect whether it is a "character file"-S: detect whether it is a "socket Tag file"-L Detection is it a symbolic link file?-e checks whether something exists! 2. The logic volume mark of the program! -G checks whether the Program executed by the GID has-O checks whether the Program executed by the UID has-p checks whether the name pipe or FIFO that transmits information between programs (to be honest, I don't know much about this !) 3. File Attribute detection! -R detection is readable Attribute-w detection is writable Attribute-x detection is executable Attribute-s detection is non-empty file-u detection is available property of "SUID"-g checks whether the property has "SGID"-k checks whether the property has "sticky bit" 4. judgment and comparison between two archives; for example, [test file1-nt file2]-nt the first file is newer than the second file-ot the first file is older than the second file-ef the first file is the same as the second file archives (archives such as links) 5. logical "and" or "& the meaning of logical AND | the meaning of logical OR



The operator number indicates that = equals is applied to: integer or string comparison. If it is in [], it can only be a string! = Not equal to: integer or string comparison if in [], it can only be a string <less than used: integer comparison in [], cannot represent string> is greater than used: integer comparison in [] cannot be used to indicate that string-eq is equal to: integer comparison-ne is not equal to: integer comparison-lt is less than applied to: integer comparison-gt is greater than applied: integer comparison-le is less than or equal to applied to: integer comparison-ge is greater than or equal to applied to: integer comparison-both sides of a are valid (and) logical expression-a logical expression-o unilaterally established (or) logical expression-o logical expression-z Null String-n non-null string


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.

Ii. 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

Iii. 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.

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.