Shell Script Learning Note eight: Process Control

Source: Internet
Author: User

First, if else// If the Else branch does not have a statement execution, do not write this else

1. If statement

If condition

Then

Command1

Command2

Command3

...

Fi

Write a line:

if [$ (ps-ef | grep-c "SSH")-gt 1]; then echo "true"; Fi

2. If Else

If Condition

Then

Command1

Command2

...

Else

Command

Fi

#!/bin/bashnum1=num2; if test $[num1]-eq $[num2]then    echo" two numbers equal " ; Else    Echo " two numbers are not equal " ; fi outputs The following result: two numbers equal

3. If else-if else

If condition

Then

Command1

Command2

Elif condition

Then

Command

Else

Command

Fi

#!/bin/Basha=Tenb= -if[$a = =$b] Then    Echo "a equals b"elif[$a-GT $b] Then    Echo "a greater than B"elif[$a-LT $b] Then    Echo "A is less than B"Else    Echo "there are no conditions to meet"fioutputs The following result: A is less than B

Second, for Loop

For Var in item1 item2 ... itemn

Do

Command1

Command2

...

Done

Write a line

For Var in item1 item2 ... itemn; Do Command1; Command2; ... done;

#!/bin/Bash forLoopinch 1 2 3 4 5 Do    Echo "$loop"; Doneoutput The following results:1 234 5 forStrinch "This is a string" Do     Echo "$str"; Doneoutputs the following results: Thisisastring

Third, while statement

While condition

Do

Command1

Done

 while 5 ]do    echo  $cnt    'cnt++'done     echo" press <CTRL-D> exit "

 The while loop can be used to read keyboard information. In the following example, the input information is set to the variable film, press <Ctrl-D> to end the loop

echo "Press <CTRL-D> exit"

Echo-n "Enter your favorite movie name:"

While Read FILM

Do

echo "Yes! $FILM is a good movie. "

Done

Four, Infinite cycle

1.

While:

Do

Command

Done

2.

While True

Do

Command

Done

3.

for ((;;))

V. until cycle

    • Until Loop executes a series of commands until the condition is true stops
    • The Until loop and the while loop are just the opposite of the processing mode.
    • The condition can be any test condition, and the test occurs at the end of the loop, so the loop executes at least once
    • The general while loop is better than the until loop, but at some point-and in rare cases, the until loop is more useful

Until condition

Do

Command

Done

Vi. case

Case value in

Mode 1)

Command1

Command2

...

;;

Mode 2)

Command1

Command2

...

;;

Esac

#!/bin/BashEcho "Enter a number from 1 to 4"Echo "the number you entered is:"Read Anum Case$aNuminch     1)      Echo "you chose 1."      ;; 2)          Echo "you chose 2."          ;; 3)          Echo "you chose 3."          ;; 4)          Echo "you chose 4."          ;;Esac

Seven, break

Jump out of all loops (terminates all loops after execution)

     while :     Do        Echo-N"Please enter a number from 1 to 5:"Read Anum
Case$aNuminch 1|2|3|4|5) Echo "the number you entered is: $aNum" ;; *) Echo "the number you entered is not between 1 and 5! End Game"Break ;; Esac Done

Eight, continue

Jump out of this cycle

Shell Script Learning Note eight: Process Control

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.