Shell branching and looping

Source: Internet
Author: User
Tags case statement

1. Common environment variables

variable name
description
example
path

finds a collection of paths for all external commands.

If you do not have a PATH environment variable, all external commands you execute need to state their file path

If the PATH environment variable contains more than 2 paths that correspond to the same name, select the file that was found first when executing this command

Echo $PATH

path= $PATH:/usr/bin

Export PATH

shell
current default shell environment (shell program)

?

home
user home directory

A special environment variable? : It saves an integer returned to the kernel after the execution of the last command, and we usually agree that if a program executes successfully, it returns 0 to the kernel, otherwise it returns non 0, and the value is 0~255,-128~127.

2. Test Condition Testing Command

(1) Numeric comparison test $ variable Option number

Options
Meaning
-lt
Less than
-le
Less than or equal
-gt
Greater than
-ge
Greater than or equal
-eq
Judging if they are equal

(2) string problem Test $ variable Option string

"": This represents an empty string;

Options
Meaning
=
Represents equality
!=
Range
-Z
is empty
-nz
is not empty

(3) File problem Test option file name

Commonly used is the test-e file name (to determine whether the file exists);

[]: Also the conditional Test command, [: Represents an external command,]: Indicates the option, so the two front and back must be separated by a space;

3. Sequential execution

Executes in row order, consistent with the order in which the contents of the file are written;

To scan and interpret verbatim characters;

4. Branch structure

(1), if branch

if arbitrary command; then

#若干命令

Fi

#其它若干命令

#!/bin/bashread-p "Please Input userage:" Userageif test $userAge-lt 20-a $userAge-gt 18; Then echo "18<userage<20" fi

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/86/E1/wKiom1fODPuDtL5JAAA0X3-3fAo040.png-wh_500x0-wm_3 -wmp_4-s_3050987663.png "title=" Qq20160906082515.png "alt=" Wkiom1fodpudtl5jaaa0x3-3fao040.png-wh_50 "/>

If arbitrary command

Then

#若干命令

Fi

#!/bin/bashread-p "Please Input userage:" Userageif test $userAge-lt 20-a $userAge-gt and then echo "18<userag E<20 "fi

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/86/E0/wKioL1fODbCR0qFYAAA1YQcVVis612.png-wh_500x0-wm_3 -wmp_4-s_3147757160.png "title=" Qq20160906082822.png "alt=" Wkiol1fodbcr0qfyaaa1yqcvvis612.png-wh_50 "/>

if arbitrary command; then # several commands; fi

If [$userAge-gt 10-a $userAge-lt 18]; Then echo the "age too small"; Fi

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/86/E1/wKiom1fODv2wasUSAAA4o7TlnHo558.png-wh_500x0-wm_3 -wmp_4-s_707045412.png "title=" Qq20160906083352.png "alt=" Wkiom1fodv2wasusaaa4o7tlnho558.png-wh_50 "/>

If then fi belongs to the internal syntax command of Bash and is syntactically controlled. The function of FI is to end the IF statement block; then it can be written after if, but it must be preceded by a semicolon, separated from the preceding command, or it can be written under if.

If arbitrary command

Then

#若干命令

elif arbitrary command

Then

#若干命令

Fi

#!/bin/bashread-p "Please Input userage:" Userageif test $userAge-lt 20-a $userAge-gt and then echo "18<userag E<20 "elif test $userAge-gt 30then echo" Userage > "fi

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/86/E1/wKiom1fOEC3D_sHzAABOQwTzXRM321.png-wh_500x0-wm_3 -wmp_4-s_2910347391.png "title=" Qq20160906083818.png "alt=" Wkiom1foec3d_shzaaboqwtzxrm321.png-wh_50 "/>

If arbitrary command

Then

#若干命令

Else

#若干命令

Fi

#!/bin/bashread-p "Please Input userage:" Userageif test $userAge-lt 20-a $userAge-gt and then echo "18<userag E<20 "Else echo" no range "fi

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/86/E1/wKiom1fOENyCNvcuAAA8q4lG8G0967.png-wh_500x0-wm_3 -wmp_4-s_374870407.png "title=" Qq20160906084116.png "alt=" Wkiom1foenycnvcuaaa8q4lg8g0967.png-wh_50 "/>

If the condition is judged by $?, this is the internal mechanism of the IF.

(2), Case branch

The function and structure of the case branch is similar to the switch...case statement of the C language.

Case $ variable in

An expression)

Several statements

;;

An expression)

Several statements

;;

......

Esac

A case expression refers to a number of constants that can be written together (using the ' | ' Representation ', provides a wildcard asterisk number that represents the case block for the default semantics.

The string in the case expression is equal to the value of the variable (string equality judgment);

#!/bin/bashread-p "Please Input your choose:" Choosecase $choose in 1|2) echo "Choose 1 or 2";;      3) echo "Choose 3";;  *) echo "no choose";; Esac

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/86/E0/wKioL1fOEtGgcc2VAABQvB2T3cE252.png-wh_500x0-wm_3 -wmp_4-s_2648802925.png "title=" Qq20160906084926.png "alt=" Wkiol1foetggcc2vaabqvb2t3ce252.png-wh_50 "/>

5. Cyclic execution

(1), while

while arbitrary command;

#若干命令

Done

Reverse single quotation marks: Executes the contents of the inverted single quotation marks as a command, and then replaces the entire inverted single-quote content with the standard output of the command!!!

' Echo Hello '------->hello

Expr is the command to perform various operations:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/86/E0/wKioL1fOFoPBNOToAAAbHm0xKoM404.png-wh_500x0-wm_3 -wmp_4-s_2101253506.png "title=" Qq20160906090555.png "alt=" Wkiol1fofopbnotoaaabhm0xkom404.png-wh_50 "/>

#!/bin/bashindex=0while test $index-lt 10do echo $index index= ' expr $index + 1 ' done

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/86/E1/wKiom1fOFdHRTjLSAAAknZd3PPw119.png-wh_500x0-wm_3 -wmp_4-s_827238622.png "title=" Qq20160906090303.png "alt=" Wkiom1fofdhrtjlsaaaknzd3ppw119.png-wh_50 "/>


While arbitrary command

Do

#若干命令

Done

Note: The shell script does not have a do ... while loop;

(2), for

The For loop is used to traverse a string (traversed by rows), which includes a carriage return line;

For traversal name in several line strings

Do

#若干命令

Done

#!/bin/bashfor a In ' Cat./abc.txt ' #将abc a line of content in a. txt file is given to A;do Echo $adone

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/86/E1/wKiom1fOGlTCY8HWAAAtTUjbHK0732.png-wh_500x0-wm_3 -wmp_4-s_644604360.png "title=" Qq20160906092218.png "alt=" Wkiom1fogltcy8hwaaattujbhk0732.png-wh_50 "/>

6. Special Circumstances

(1), break: Jump out of the current layer of break loop;

(2), when the while encounters a pipe character

Command | While read variable name

Do

#若干命令

Done

The above loop allows you to traverse the standard output produced by the command before the pipe character, where the variable name holds the contents of a row.

#!/bin/bashfind/usr/include-name "W*.h" |    While read Filenamedo cmd= ' echo cp $filename./' #将 ' in the standard output to CMD; $cmd #每次将一个文件拷贝到当前目录下;d One #ls-1 is a vertical print file name

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/86/E1/wKioL1fOHTTg_QwwAABjhKgkph8559.png-wh_500x0-wm_3 -wmp_4-s_191571173.png "title=" Qq20160906093435.png "alt=" Wkiol1fohttg_qwwaabjhkgkph8559.png-wh_50 "/>

(3), when the while encounters the redirect input

While read variable name

Do

#若干命令

Done < files

is to read the contents of the file from one line of the file;

#!/bin/bashwhile Read Filenamedo echo $filenamedone <./abc.txt

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/86/E1/wKioL1fOH_zh006UAABUOq4Mp44751.png-wh_500x0-wm_3 -wmp_4-s_426967636.png "title=" Qq20160906094625.png "alt=" Wkiol1foh_zh006uaabuoq4mp44751.png-wh_50 "/>

Equivalent to the following notation:

For variable name in ' Cat file name '

Do

#若干命令

Done



This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1846675

Shell branching and looping

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.