Shellwhile Loop, c-for Loop, until loop, case branch structure, flow control statement

Source: Internet
Author: User

While loop structure

While condition-determining expression

Do

Statement blocks executed when the condition is judged

......

......

Done


Example: Using while to output a number 1 to 10 (ascending output)

J=1

While [$j-le 10]

Do

#useradd wsyht$j

#userdel-R wsyht$i

Echo $j

Let J + + #j =j+1

Done


C-FOR Cycle Structure

for (assigning initial value; condition judgment; step))

Do

Actions performed when the condition is judged

......

Done


For ((i=1;i<=5;i++))

Do

echo "$i"

Sleep 1

Done


<=

<

>=

>


Until cycle structure (the condition judgment is not established to execute the loop body, judging the condition set up cycle termination)

Until conditional-judgment expression

Do

Loop body

.....

Done


Until [1-eq 1]

Do

echo "Wsyht"

Sleep 1

Done



True constancy

FALSE constant false


#!/bin/bash

#while true

#until false

While:

Do

#echo "Yht"

#sleep 1

Done


: The position of the conditional expression is constant, and in the body of the loop it means nothing.



Case Branching structure

Execute different commands according to the different values of the variables

Name=yht


Case variable name in

Mode 1)

The action performed when the value of the variable matches the pattern 1;;

......

Mode 2)

The action performed when the value of the variable matches the pattern 2;;

......

Mode 3)

The action performed when the value of the variable matches the pattern 3;;

......

*)

The action performed when the value of the variable does not match any of the above modes;

......

Esac


Cases:

#!/bin/bash

Read-p "Please enter the letter of your choice a | B | C | D "Zimu

Case $zimu in

A

echo "You entered the letter a";;

B

echo "You entered the letter B";;

C

echo "You typed the letter C";;

D

echo "You entered the letter D";;

*)

echo "$ only Select a | B | C | D in one of the "

Esac



Case is usually used with a function to write a startup script to the service



Process Control Statements

Break continue operation pair like a looping structure


Break

Jump out of the current loop body


Continue

End this cycle and start the next cycle


Exit

Exit script default return value is 0


Return

return value only, no exit, use in function, set return value of function


Shift (position move function)

Used to migrate location variables, pass $1-$N values to the left, and delete variables with no value


Example 1:

$ $

Ten shift

Shift

Shift


Example 2: Example 3:

#!/bin/bashi=0

Echo $#while [$#-GT 0]

echo $ do

Shifti= ' expr $i + $ '

echo $#shift

echo $ $ done

ShiftEcho $i

Echo $#

Echo $ $


Cases:

#!/bin/bash

For ((i=1;i<=9;i++))

Do

If [$i-eq 9]

#break

#continue

#exit 2

Fi

Echo $i

Done


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1786326

Shellwhile Loop, c-for Loop, until loop, case branch structure, flow control statement

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.