Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Linux shell process Control

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Linux shell process control case analysis". In daily operation, I believe many people have doubts about Linux shell process control case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Linux shell process control case analysis". Next, please follow the editor to study!

I. shell conditional statement (if usage)

If statement structure [if/then/elif/else/fi]

If conditional test statement thenaction [Elif conditional actionelseaction] fi

The shell command, which can be divided by semicolon or newline character. If you want to write more than one command per line, you can split it by "';", such as:

[chengmo@centos5] $axi5bot 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; II, Loop statement (for,while,until usage):

(1) for recycling method (for/do/done) 1.for … In statement-- grammatical structure

For variable in seq string # seq string as long as it is separated by space characters, each time for... When in reads, the values are read sequentially to the preceding variables. Doactiondone

Instance (testfor.sh):

#! / bin/shfor I in $(seq 10); do # seq 10 produces 1 2 3. 10 space delimited string echo $ibot done

2.for ((assignment; condition; Operation statement))

For ((assignment; condition; Operation statement)) doactiondone

Instance (testfor2.sh):

#! / bin/shfor ((iFix _ 1 _ idoecho $I _ done

(2) while recycling (while/do/done)

While conditional statement doactiondone

Example 1:

#! / bin/shi=10;while [[$I-gt 5]]; doecho $I; ((iMel -)); done

Running result:

Sh testwhile1.sh109876

Example 2: (read the file contents in a loop:)

#! / bin/shwhile read line;doecho $line;done

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

(3) until Loop statement-- grammatical structure

Until condition # exits until the condition is met. Otherwise, execute action.doactiondone

Instance (testuntil.sh):

#! / bin/sha=10;until [[$a-lt 0]]; doecho $a; ((a -)); done

Results:

Sh testuntil.sh109876543210 III. Shell select statements (case, select usage)

(1) case select statement uses (case/esac)-syntax structure

Case $arg inpattern | sample) # arg inpattern or sample;;pattern1) # arg in pattern1;;*) # default;;esac

Description: pattern1 is a regular expression, you can use the following characters: * any string? Any character [abc] a, b, or c, one of the three characters [a Murn] any character from a to n | multiple selections

Example:

#! / bin/shcase $1 instart | begin) echo "start something";; stop | end) echo "stop something";; *) echo "Ignorant";; esac

Running result:

Testcase.sh startstart something

(2) how to use select statements (generate menu selections)-syntax

Select variable name in seq variable doactiondone

Example:

#! / bin/shselect 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:

At this point, the study on "Linux shell process control case analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report