Shell condition nesting (if conditional statement)

Source: Internet
Author: User

"Note 1": unlike Java, PHP and other languages, SH's process control can not be empty, such as:

The code is as follows:

<?php
if (isset ($_get["Q"])) {
    search (q);
}
else {
    //do nothing
}
?>


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

If condition
then
    command1
    command2
    ...
    CommandN
fi

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

if test $[2*3]-eq $[1+5]; Then echo ' The two numbers are equal! '; Fi
Other words
If condition
Then
can also be written
If Condition;then

"NOTE 2":If the condition must be a conditional statement, the result should be true or false, although we often consider 1 to be true and 0 to be false, but the condition operation here can only be true or false, otherwise Even if the execution result is 1 or 0, it is assumed that the condition condition is available, and the other branches will not be left. For example:
a=1.2
b=2.3
c=3.4
If [1-eq 0]
then
  echo aaaaaaaaaa
fi

  If [' echo ' $a > $c ' |BC '-ne 0]
  then
        echo "Max is a"
  else
        echo "Max is C"
  fi

If [' echo ' $a > $c ' |BC ']; then
        echo Max is a '
  else
        Echo ' Max is C '
  fi

The above script, the result: Max is C
Max is a
Here's the note.
' echo ' $a > $c "|BC '  and <span style=" font-family:arial, Helvetica, Sans-serif; The results of the > ' echo ' $a > $c ' |BC '-ne 0 are 0 and false respectively, but if the result 0 is True,false or false.



1, if format

If [condition]--note that there are spaces on both sides of the parentheses, condition is a conditional expression
Then
Commands
Fi

Function: Judge whether the condition condition is set up, if set up, carry out the middle order commands, do not set up not to execute.

such as: if [$a-gt $b]
Then
echo "A is greater than B"
Fi


If the conditional expression (such as the IF [$a-gt $b]) can be followed, or a command (such as an if mkdir/abc) can be directly followed, the execution result of the command is judged, and if executed successfully, it is equivalent to the condition, and if the execution is unsuccessful, the condition is not tenable.

2, if else format

If condition
then
    command1
    command2
    ...
    CommandN
Else
    command
fi

3. If else-if else format

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:

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

Output:
The two numbers are equal!


4. If nesting

Format one:

if [condition]
Then
if [condition]
Then
Commands1
Else
Commands2
Fi
Fi

Format two:

if [condition]
Then
if [condition]
Then
Commands1
Else
Commands2
Fi
Else
Commands3
Fi

5, the multiple conditions indicated:

    logic and
    if [condition1-a condition2]  
  or   if [condition1] && [Condition2]


    logic or
    if [condition1-o condition2]  < br>  or   If [Condition1] | | [Condition2]

    logical non (reverse)
    !  

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.