Shell script If Else statement summary _linux Shell

Source: Internet
Author: User

Unlike Java, PHP and other languages, the process control of SH is not NULL, such as:

Copy Code code as follows:

<?php
if (Isset ($_get["Q"])) {
Search (q);
}
else {
Doing nothing
}
?>

You can't write this in Sh/bash, if the Else branch has no statement execution, don't write this else, like this:

Copy Code code as follows:

If condition
Then
Command1
Command2
...
CommandN
Fi

Of course, you can also write a line (for the terminal command prompt), like this:

Copy Code code as follows:

if test $[2*3]-eq $[1+5]; Then echo ' The two numbers are equal! '; Fi

The end of the fi is the if reverse spelling, you will encounter similar.

If Else format

Copy Code code as follows:

If condition
Then
Command1
Command2
...
CommandN
Else
Command
Fi

If else-if else format

Copy Code code as follows:

If Condition1
Then
Command1
Elif Condition2
Command2
Else
CommandN
Fi

The If Else statement is often used in conjunction with the Test command, as follows:

Copy Code code as follows:

NUM1=$[2*3]
NUM2=$[1+5]
if test $[num1]-eq $[num2]
Then
Echo ' The two numbers are equal! '
Else
Echo ' The two numbers are not equal! '
Fi

Output:
The two numbers are equal!

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.