Chapter 4 control flow of Shell

Source: Internet
Author: User
Tags case statement

Chapter 4 Control Flow Structure

1. If statement
If condition 1;

Then
Command 1
Elif condition 2;
Then
Command 2
Else
Command 3
Fi

If condition
Then command
Fi
**************************************** ***********************
#! /Bin/bash
If ["10"-lt "12"]
Then
Echo "Yes, 10 is less than 12"
Else
Echo "No !! "
Fi

**************************************** ***********************

Various forms of conditions

Expression
! Expression: No
Expression1-A expression2 is equivalent &&
Expression1-O expression2 |
-N string length is not empty
-Z string ch 0
String = string
String! = String
Integer1-EQ integer2
Integer1-ge integer2 is equivalent to>
Integer1-Le integer2 is equivalent to <=
Integer1-lt integer2 is equivalent to <
Integer1-ne integer2 is equivalent! =
File1-Ef file2 file1 and file2 have the same number of devices and nodes
File1-nt file2 file1 is newer than fiel2
File1-ot file2 file1 is older than file2
-B file exists and is a block Device
-C file exists and is a character device
-D file exists and is a directory
-E file exists
-F file: Rule file exists.
-G File
-G File
-K file
-L file: connection file
-O has real users
-P file
-R file readable
-W file writable
-X file executable
**************************************** ***********************
#! /Bin/bash
Echo-n "enter your name :"
Read name
If ["$ name" = ""]; then
Echo "you do not enter anything"
Fi
**************************************** ***********************
**************************************** ***********************
#! /Bin/bash
If CP myfile. Bak myfile; then
Echo "Good Copy"
Else echo "'basename $ 0': erro cocould not copy"> & 2
Fi
**************************************** ***********************
**************************************** ***********************
#! /Bin/bash
Echo-n "enter your name :"
Read name
If [-Z $ name] | ["$ name" = ""]; then
Echo "You did not enter a name ."
Elif ["$ name" = "chinalab"]; then
Echo "Hello root"
Elif ["$ name" = "chinalab"]; then
Echo "Hello chinalab"
Else
Echo "You are not root chinalab, but hi, $ name"
Fi

Note: spaces must be left on both sides of the equal sign.

**************************************** ***********************

2. Case statement
Multiple selection statements, which can match a value with a pattern

Case value in
Mode 1)
Command 1 ;;
Mode 2)
Command 2 ;;
Esac

The case value must be followed by the word in, and each pattern ends with parentheses. The value can be a variable or constant. After matching finds that the value matches a certain mode, all commands are executed.
**************************************** ***********************
#! /Bin/bash
Echo-n "enter a number from 1 to 3 :"
Read ans
Case $ ans in
1)
Echo "you selected 1"
;;
2)
Echo "you selected 2"
;;
3)
Echo "you selected 3"
;;
*)
Echo "'basename $ 0': This is not between 1 and 3"> & 2
Exit;
;;
Esac
**************************************** ***********************

3. For Loop

For variable name in list
Do
Command 1
Command 2
Done
When the variable value is in the list, the for loop executes all the commands once and uses the variable name to access the values in the list. Command can be any valid shell command and statement. The in list method is optional. If you do not need it, the for loop uses the location parameter of the command line.

**************************************** ***********************
#! /Bin/bash
For Loop in 1 2 3 4 5
Do
Echo $ Loop
Done
**************************************** ***********************
#! /Bin/bash
For Loop in "orange red bue gray"
Do
Echo $ Loop
Done
**************************************** ***********************
4. Until Loop

Until Condition
Do
Command 1
Command 2
...
Done

Note: If the condition is any test condition, the test occurs at the end of the loop, so the loop is executed at least once.
**************************************** ***********************
#! /Bin/bash
Part = "/backup"
Look_out = 'df | grep "$ part" | awk '{print $5}' | SED's/% // g''
Echo $ look_out
Until ["$ look_out"-GT "90"]
Do
Echo "filesystem/backup is nearly full" | mail Root
Look_out = 'df | grep "$ part" | awk '{print $5}' | SED's/% // g''
Sleep 3600
Done
**************************************** ***********************
5. While Loop

While command
Do
Command 1
Command 2
...
Done
**************************************** ***********************
#! /Bin/bash
Echo "press <Ctrl> + D to exit the input"
While echo-n "Enter your favorite movie:"; read Film
Do
Echo "yeah, $ {film} is a good movie! "
Done
**************************************** ***********************
#! /Bin/bash
While read line
Do
Echo $ line
Done <reads.txt
**************************************** ***********************

6. Break and continue

Break [N] exits the loop. N indicates the number of layers to exit the loop.
Continue skip loop step

**************************************** ***********************
#! /Bin/bash
While:/*: always true */
Do
Echo-n "enter any number [1... 5]:"
Read ans
Case $ ans in
1 | 2 | 3 | 4 | 5)
Echo "you enter a number between 1 and 5 ."
;;
*)
Echo "Wrong number, bye"
Break
;;
Esac
Done
**************************************** ***********************
#! /Bin/bash
Echo "Please input a number between 1 to 5"
While:
Do
Read ans
Case $ ans in
1 | 2 | 3 | 4 | 5)
Echo "the number you input is: $ ans"
;;
*)
Echo "Wrong number, But, will you continue? "
Read is_continue
Case $ is_continue in
Y | Yes | yes)
Continue
;;
*)
Break;
;;
Esac
Esac
Done
**************************************** ***********************

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.