Shell Programming-control structure

Source: Internet
Author: User
Tags echo command

1.shell condition judgment

Command: Test or [

Use "[" to End With "]" and a space between judgment statements

such as: Determine if the demo.c file exists

If [-F DEMO.C]

Then

...

Fi

Or

If Test–f demo.c

Then

...

Fi

1.1 String Comparisons

string1 = string2 Same as True

String1! = string2 different for true

-n string string not NULL for true

-Z string is null true

1.2 Arithmetic comparisons

Exp1–eq Exp2 equal to True

Exp1–ne Exp2 is not equal to true

EXP1–GT EXP2 Exp1 greater than EXP2 is true

Exp1–ge EXP2 exp1 greater than or equal to EXP2 true

Exp1–lt exp2 Exp1 Less than EXP2 is true

Exp1–le EXP2 exp1 less than or equal to EXP2 true

! EXP Reverse, exp result false result for true

1.3 File Condition Test

-D file If it is a directory then the result is true

-e file if the files exist, the result is true, the-e option is not portable in history, usually with-F instead

-F File The result is true if it is a normal file

-G file If the Set-group-id bit is set, the result is true

-r file if the document is readable the result is true

-S file if the size of the files is not 0 The result is true

-u file if the Set-user-id bit is set, the result is true

-W file if the document is writable the result is true

-X file If the result is true without an executable

2 Control structure

2.1 If statement

If condition1then     statements1elif condition2then statements2Else  Statements3fi         

2.2 For

In valuesdoes     statements    Done

By default, all shell variable values are considered strings, and for is especially useful for looping through a series of strings.

2.3 While

For not knowing the number of cycles

Do    statements  done

2.4 until

Same as while

Until conditiondo    statement    

2.7 Case

In   pattern [| pattern] ...) statements;;   pattern [| pattern] ...) statements;; ... Esac   

Example 1

#! /bin/ShEcho"Is it morning? Enter Yes or no"Read TimeOfDayCase"$timeofday"InchYesEcho"Morning ";; NO) echo  "afternoon "; y) echo " morning "; N) echo  "afternoon ";; *) echo " default, no Patternesacexit 0 



Example 2
#! /bin/SHEcho "Is it morning? Enter Yes or no"Read TimeOfDay Case "$timeofday" inchYes| y | Yes | YES)Echo "Morning";; N* | N)Echo "Afternoon";; * )Echo "Not recognizied";; EsacExit0

Each case entry statement needs to use ";;" As a terminator, but not required, the last case entry can omit ";;", the case executes the first match it finds instead of the best match, so the best match is generally placed at the very beginning.




2.8 and List

Statement1 && statement2 && statement3 && ...

The logic of execution is that the latter command executes only if all previous commands succeed, executes each command from the left, and if a command returns TRUE, the statement on the right can execute until a command returns FALSE, or all the commands of the list are executed.

#! /bin/sh

Touch File_one

Rm-f File_two

If [-f File_one] && echo "Hello" && [-F File_two] && echo "There" then echo "in if" else EC Ho "in Else" fi

Exit 0

Output:

Hello
In Else
Resolution: The Touch and RM commands ensure that the relevant files in the current directory are in a known state, and then the && list executes the [-F File_one] statement, returns TRUE, continues the echo "Hello", and the Echo command always returns TRUE, continuing with the third Test [-F File_two], test failed, execution ended, and list returned FALSE,IF statement executes its else part

2.9 or List

Statement1 | | Statement2 | | Statement3 | | ...

Executes each command from the left, and if a command returns false, the command on the right can be executed until a command returns TRUE, or all commands are executed.

#! /bin/sh

Rm-f File_one

If [-f File_one] | | echo "Hello" | | echo "There" then echo "on if" else echo "in Else" fi

Exit 0

Output:

Hello
In If

Analytical:

[-F File_one] execution failed, continue executing echo statement, return True, subsequent command does not need to execute, or list returns TRUE,IF statement execution then part.

Shell Programming-control structure

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.