Linux shell Process Control

Source: Internet
Author: User

Linux shell Process Control
GuideUnlike Java, PHP, and other languages, 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 thenaction [elif condition actionelseaction] fi

Shell commands can be separated by semicolons or line breaks. If you want to write multiple commands in one line, you can use "';" to separate them, for example:

[chengmo@centos5 ~]$ 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;

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

(1) for Loop usage (for/do/done)
1.... In statement-syntax structure

For variable in seq string # 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. Doactiondone

Instance (testfor. sh ):

#! /Bin/shfor I in $ (seq 10); do # seq 10 generates 1 2 3 ...... 10-space separator string echo $ I; done;

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

For (assign value; condition; Operation statement) doactiondone;

Instance (testfor2.sh ):

#!/bin/shfor((i=1;i<=10;i++));doecho $i;done;

(2) while loop usage (while/do/done)

While Condition Statement doactiondone;

Instance 1:

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

Running result:

sh testwhile1.sh109876

Example 2: (Read File Content cyclically :)

#!/bin/shwhile read line;doecho $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

(3) until loop statement-syntax structure

Until condition # exit after the condition is met. Otherwise, execute action. doactiondone.

Instance (testuntil. sh ):

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

Result:

sh testuntil.sh109876543210
Iii. shell selection statement (case and select usage)

(1) Use case/esac statements -- syntax structure

case $arg inpattern | 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/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 the select statement (generate menu selection) -- syntax

Select variable name in seq variable doactiondone

Instance:

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

From: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html

Address: http://www.linuxprobe.com/shell-process-control.html


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.