Shell script Programming---statement control in Linux

Source: Internet
Author: User
Tags case statement

Overview:

Statement control for Shell scripts

-There are three processes in the process programming language mentioned earlier:

Sequential execution: Executed in the order of a single statement;

Select execution: Select execution according to the conditions;

Loop execution: Loop execution According to the given loop condition.

-Where the order does not require specific control, just follow the statement in sequence, select execution, you need a specific control statement (such as: if,case) to judge execution, loop execution requires specific loop control control (such as: For,while, etc.).


First, Use the Read command to accept input

-During bash scripting, sometimes the program needs to interact with the user , such as prompting the user to enter some parameters, we can use the Read command to complete this function

- Syntax:read [option] ... [Name]

- P: "Description Information" (indicates the prompt to be displayed)

-t:timeout

Note: read reads the values from the standard input, assigns a variable to each word, and all the remaining words are assigned to the last variable

Example: Read-p "Enter a filename:" FILE


Second, conditional Select if statement

- Select execute:

NOTE: If statements can be nested (if is a keyword, not a command.) )

Single Branch

if judgment condition; Then

Branch code with true condition

fi

Dual Branch

if judgment condition; Then

Branch code with true condition

Else

The condition is a false branch code

fi

- Multi-branch

if CONDITION1; Then

If-true

elif  CONDITION2; Then

If-ture

elif CONDITION3; Then

If-ture

...

Else

All-false

fi

judge from top to bottom, execute its branch when first encountering the "true" condition, and then end the entire if statement

Example:

execute commands According to the exit status of the command

if ping-c1-w2station1 &>/dev/null; Then

Echo ' Station1 is up '

elif grep "Station1" ~/maintenance.txt &>/dev/null; Then

Echo ' Station1 is under Go ingmaintenance '

Else

Echo ' station1isunexpectedlydown! ' exit1

fi


Conditional Judgment Case Statement (mainly used for the matching of discrete values )

- case Variable reference in

PAT1)

Branch 1

;;

PAT2)

Branch 2

;;

...

*)

Default Branch

;;

Esac

-Casesupports GLOB-style wildcard characters:

*: Any character of any length

?: any single character

[]: Any single character in the specified range [ADC] (can also represent range [1-10])

A|b:a or B



Note : 1.if statement in the use of the process, you can achieve multiple nesting, here must pay attention to the format of writing, sub-level, do not write Chaos

2.case generally in the matching of discrete values used more, be sure to pay attention to the writing format, do not write the wrong

3. When writing scripts, do not worry about the idea of fresh, every situation should be taken into account.



Practice:

1, write a script/root/bin/createuser.sh, to achieve the following functions: Use a user name as a parameter, if the specified parameter of the user exists, it will show its existence, otherwise added; Displays the ID number of the added user and other information

650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160816/1471330334984405.png "title= "1471330334984405.png" alt= "Practice 1.png"/>

2, write a script/root/bin/yesorno.sh, prompting the user to enter Yes or no, and to determine whether the user entered Yes or no, or other information

650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160816/1471330355394886.png "title= "1471330355394886.png" alt= "Practice 2.png"/>

3, write a script/root/bin/filetype.sh, determine the user input file path, display its file type (normal, directory, link, other file type)

4, write a script/root/bin/checkint.sh, determine whether the user input parameter is a positive integer

650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160816/1471330389898255.png "title= "1471330389898255.png" alt= "Practice 4.png"/>



Shell script Programming---statement control in Linux

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.