Conditional judgment, looping statements, and instances of Linux shells

Source: Internet
Author: User

Two special devices for shell condition determination


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


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=" "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:


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


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


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


650) this.width=650; "src=" Http:// "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


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 #!/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=" "title=" 2.jpg " alt= "Wkiom1x8fp3ijhsxaaheyqtz2ao252.jpg"/>

650) this.width=650; "src=" "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=" "title=" 4.jpg " alt= "Wkiom1x8klyic9pyaafrd2x5z6w221.jpg"/>

[[Email protected] bashtest]# sh 2500[[email protected] bashtest]# cat #!/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}


Looping commands


For loop with no list

Format: for Var


Looping commands


While loop:

While Condtion;


Loop body


Entry condition: When condition is "true";

Exit Condition: When condition is "false";



Loop body

Correction expressions for control variables






650) this.width=650; "src=" Http:// "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




650) this.width=650; "src=" Http:// "title=" Qq20150919130617.jpg "alt=" Wkiom1x87v2sqtpdaadij_jjrpi272.jpg "/>

[[email protected] bashtest]# cat #!/bin/bashdeclare -i sum= 0declare -i i=1until [[  "$i"  -gt 10 ]]dolet sum+=ilet i++doneecho   $sum [[Email protected] bashtest]# sh -x + 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:




Break [n]



[[email protected] bashtest]# cat #!/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 + 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]# 




Continue [n]




650) this.width=650; "src=" Http:// "title=" Qq20150919133300.jpg "alt=" Wkiom1x886qrcgntaai90jrubr0002.jpg "/>

Dead Loop:

While true; Do

Loop body

if condtion; Then




until false; Do

Loop body





650) this.width=650; "src=" Http:// "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:// "title=" Qq20150919150537.jpg "alt=" Wkiom1x9cwpjqzanaai0ulajyxu354.jpg "/>

Special usage for the FOR loop:

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

Loop body


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:// "title=" Qq20150919151123.jpg "alt=" Wkiom1x9cqhaqxtraaeovdbf9tg334.jpg "/>

Conditional judgment, looping statements, and instances of Linux shells

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.