Conditional judgment, looping statements, and instances of Linux shells

Source: Internet
Author: User

Two special devices for shell condition determination

/dev/null

The empty device of a Linux system, also known as a bit bucket, is discarded when you do not want the normalized output to be displayed or saved to a file to be directed to/dev/null

Prohibit standardized output cat $filename >/dev/null

Prohibit normalization error RM $filename >/dev/null

/dev/zero

Linux input device, you can use his initialization file, can be unlimited output 0, another role is to use 0 to fill a file of a specified size

In a conditional judgment statement && represents and | | Represents or

Script Location Parameters:

In Linux, the parameter location from the command after the next, respectively, is 0,1,2,3.

Among the special variables are:

$?: The status result of the command;

$#: The number of arguments passed to a script or function;

$* and [email protected]: reference to the parameter list passed to the script or function;

$$: The process generated when the script executes

Shift [N]: On behalf of rotation

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/5F/wKioL1X7piOj9GodAAMn3rOgScg629.jpg "title=" 2.jpg " alt= "Wkiol1x7pioj9godaamn3rogscg629.jpg"/>

Interacting with the User:

Read for reading variables from the keyboard

Format: Read [-pt] Value

-P: followed directly with prompt characters

-T: The number of seconds after which the wait is directly followed

[[email protected] bashtest]# echo $a testwhoaaaaa test[[email protected] bashtest]# read-p "input:"-T 5 ainput:aaaaa[[e Mail protected] bashtest]# echo $aaaaaa [[email protected] bashtest]#

Command Reference

Format one: ' Command '

Format two: $ (command)

Conditional Judgment Statement If,then,case

There is only one statement format:

if[condition];then

Command ==> when the condition is true, if more than one line is separated by a newline character

Fi

When a judgment is required:

if [Condition];then

What you need to enter when the condition is set up

elif [Condition Two];then

What you need to enter when the condition is set up

Else

What needs to be entered when conditions and conditions one or two are not established

Fi

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/61/wKiom1X7l9eyQsG8AAKdU20_sRM359.jpg "title=" 1if.jpg "alt=" Wkiom1x7l9eyqsg8aakdu20_srm359.jpg "/>

The case's judgment structure can also be used for multiple branches:

Syntax format:

Case $ variable name in

"First variable content") command order;;

"Second variable content") command order;;

"Third variable Content") command order;;

"IV Variable Content") command order;;

...

*) command order;; = = = "Performed under conditions that do not match the above conditions

Esac

Looping statements: for and while until

In the Linux loop statement, the list is a focus, he is responsible for the loop when the element is required to be separated by one or more spaces or newline character string, the list of each string is assigned to the variable represented by variable;

Build format:

Number: ①list= "1 2 3 4 5 6 10" = = "for var in ${list}
②for var in {1..10}

[email protected] bashtest]# Cat 1for.sh #!/bin/bash# #list = "1 2 3 4 5 6" #第一种 #for i in ${list} #do # echo $i #done=== ================================for i in {1..10} #第二种do echo $idone [[email protected] bashtest]#

(2) give the list directly
(3) Glob

#!/bin/bash # for filename in/relative specific path/*; Do file $filename done Note: If it is not a relative specific path, then the script will error, filename only searches the contents of the relative one-level path does not parse the second-level subdirectory

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/67/wKiom1X8Fp3iJHSXAAHEyQtz2ao252.jpg "title=" 2.jpg " alt= "Wkiom1x8fp3ijhsxaaheyqtz2ao252.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/67/wKiom1X8FeLCn9P4AAEUpXF1PKw863.jpg "title=" 3.jpg " alt= "Wkiom1x8felcn9p4aaeupxf1pkw863.jpg"/>

(4) Command generation

Arithmetic operations:

+, -, *, /, %, **


(1) $[$A + $B]

(2) $ (($A + $B))

(3) Let variable= $A + $B

(4) variable=$ (expr $A + $B)

Enhanced Assignment:

+=

sum=$[$sum + $i]

Let sum+= $i

-=, *=, /=, %=


Let count=$[$count +1]--let count+=1 and let count++

Let count=$[$count-1]-and let Count-=1 count--

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/68/wKiom1X8KLyic9pyAAFRd2x5Z6w221.jpg "title=" 4.jpg " alt= "Wkiom1x8klyic9pyaafrd2x5z6w221.jpg"/>

[[Email protected] bashtest]# sh 4for-2.sh 2500[[email protected] bashtest]# cat 4for-2.sh #!/bin/bashsum=0for i in $ (seq 1 2) Dolet "sum+=i" Doneecho $sum [[email protected] bashtest]#
For pre-run test statements for creating repeating loops

Format: for Var in ${list}

Do

Looping commands

Done

For loop with no list

Format: for Var

Do

Looping commands

Done

While loop:

While Condtion;

Do

Loop body

Done

Entry condition: When condition is "true";

Exit Condition: When condition is "false";

While CONDITION;

Do

Loop body

Correction expressions for control variables

Done

While:

Do

Command

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/6A/wKiom1X86DPw_IMhAAG6MLCYGFo909.jpg "title=" Qq20150919124422.jpg "alt=" Wkiom1x8l67bny-paahees1exc4666.jpg "/>

Until cycle:

The until command is similar to the while command, while the script until that can be implemented can also be implemented, but the difference is that the exit state of the until loop is not 0 and the exit State is 0 (as opposed to while). That is, the Whie loop resumes execution while the condition is true and until executes the loop when the condition is false.

Syntax format:

Until expression

Do

Command

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/6A/wKiom1X87V2SQtPDAADij_jjRPI272.jpg "title=" Qq20150919130617.jpg "alt=" Wkiom1x87v2sqtpdaadij_jjrpi272.jpg "/>

[[email protected] bashtest]# cat 1until.sh #!/bin/bashdeclare -i sum= 0declare -i i=1until [[  "$i"  -gt 10 ]]dolet sum+=ilet i++doneecho   $sum [[Email protected] bashtest]# sh -x 1until.sh + declare -i  sum=0+ declare -i i=1+ [[ 1 -gt 10 ]]+ let sum+=i+  let i+++ [[ 2 -gt 10 ]]+ let sum+=i+ let i+++ [[  3 -gt 10 ]]+ let sum+=i+ let i+++ [[ 4 -gt 10  ]]+ let sum+=i+ let i+++ [[ 5 -gt 10 ]]+ let sum+ =i+ let i+++ [[ 6 -gt 10 ]]+ let sum+=i+ let i+++  [[ 7 -gt 10 ]]+ let sum+=i+ let i+++ [[ 8 -gt 10  ]]+ let sum+=i+ let i+++ [[ 9 -gt 10 ]]+ let sum+=i+ let i+++  [[ 10 -gt 10 ]]+ let sum+=i+ let i+++ [[ 11 -gt 10  ]]+ echo 5555[[email protected] bashtest]#

Loop control:

Continue [n]: End the cycle in advance, and go directly to the next round;

Break [n]: End cycle prematurely;


While loop:

While CONDITION; Do

.......

if CONDITION2; Then

Break [n]

Fi

Done

[[email protected] bashtest]# cat 1until.sh #!/bin/bashdeclare -i sum= 0declare -i i=1until [[  "$i"  -gt 10 ]]dolet sum+=ilet i++if  [  $i  -eq 5 ]; then   breakfiecho  $sumdone [[email protected]  bashtest]# sh -x 1until.sh + declare -i sum=0+ declare -i  i=1+ [[ 1 -gt 10 ]]+ let sum+=i+ let i+++  ' ['  2  -eq 5  '] ' + echo 11+ [[ 2 -gt 10 ]]+ let sum+=i+  let i+++  ' ['  3 -eq 5  '] ' + echo 33+ [[ 3 -gt  10 ]]+ let sum+=i+ let i+++  ' ['  4 -eq 5  '] ' + echo  66+ [[ 4 -gt 10 ]]+ let sum+=i+ let i+++  ' ['  5 -eq  5  '] ' +  break[[email protected] bashtest]# 

While CONDITION; Do

......

if CONDITION2; Then

Continue [n]

Fi

......

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/6A/wKiom1X886qRcGnTAAI90JruBr0002.jpg "title=" Qq20150919133300.jpg "alt=" Wkiom1x886qrcgntaai90jrubr0002.jpg "/>

Dead Loop:

While true; Do

Loop body

if condtion; Then

Break

Fi

Done


until false; Do

Loop body

if CONDITION; Then

Break

Fi

Done

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/68/wKioL1X8-vbidepYAAZVO0-2Q2M235.jpg "title=" Qq20150919135501.jpg "alt=" Wkiol1x8-vbidepyaazvo0-2q2m235.jpg "/>

Special usage of the while loop:

Traverse each line of the file:

while read VARIABLE; Do

Loop body

Done </path/from/some_file

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/6B/wKiom1X9CWPjqzanAAI0UlajyxU354.jpg "title=" Qq20150919150537.jpg "alt=" Wkiom1x9cwpjqzanaai0ulajyxu354.jpg "/>

Special usage for the FOR loop:

For ((EXPR1;EXPR2;EXPR3)); Do

Loop body

Done


EXPR1: Defines the control variable and assigns the initial value;

EXPR2: cyclic control conditions;

Entry condition: Control condition is "true"

Exit Condition: Control condition is "false"

EXPR3: Correcting control variables

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/6B/wKiom1X9CqHAqxTRAAEoVdbF9tg334.jpg "title=" Qq20150919151123.jpg "alt=" Wkiom1x9cqhaqxtraaeovdbf9tg334.jpg "/>

Conditional judgment, looping statements, and instances of Linux shells

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.