Shell script Programming--if statements

Source: Internet
Author: User

How to handle the programming logic:
1, sequential execution
2, select Execute
3, loop execution

Conditional Select if statement
Select Execute:
Note: If statements can be nested

Single Branch
if judgment condition; then
Branch code with true condition
Fi

Example code:

1 #!/bin/bash
2
3 #Author: Wangjun
4 #Version: 1.0
5 #Create time:2016-08-13 19:40:55
6 #Description: Ifsingle test
7
8 Read-p "Please input a username:" Name
9 If ID $name &>/dev/null; then
echo "$name user exists,user ID: ' Id-u $name '"
One fi

650) this.width=650; "title=" Ifsingle.png "alt=" Wkiol1evdeihhdfpaabqmefctau994.png "src=" http://s3.51cto.com/ Wyfs02/m01/85/ec/wkiol1evdeihhdfpaabqmefctau994.png "/>


Dual Branch
if judgment condition; then
Branch code with true condition
Else
The condition is a false branch code
Fi

Example code:

1 #!/bin/bash
2
3 #Author: Wangjun
4 #Version: 1.0
5 #Create time:2016-08-13 20:00:00
6 #Description: Ifdouble test
7
8 Read-p "Please input a username:" Name
9 If ID $name &>/dev/null; then
echo "$name user exists,user ID: ' Id-u $name '"
One else
echo "$name user doesn ' t exist"
Fi

650) this.width=650; "title=" Ifdouble.png "src=" http://s1.51cto.com/wyfs02/M02/85/EC/ Wkiom1evd8lwcz5jaabbhv0mdku780.png "alt=" Wkiom1evd8lwcz5jaabbhv0mdku780.png "/>

Multi-Branch
if CONDITION1; Then
If-true
Elif CONDITION2; Then
If-ture
Elif CONDITION3; Then
If-ture
...
Else
All-false
Fi

Judge by condition, when the first encounter is a "true" condition, execute its branch, and then end the entire if statement

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1837637

Shell script Programming--if statements

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.