If-then and if-then-else statements

Source: Internet
Author: User

Tag: Shell

1. Use the if-then statement

In structured commands, the most basic type is the if-then statement. The if-then statement has the following format:

If commandthen commandsfi
If you are using the if-then statement in other programming languages, this form may confuse you. In other programming languages, the object after the if statement is an equality to test whether the value is true or false. If the exit status code of the command is 0 (the command is successfully run), the command in the then part will be executed. If the exit status code of this command is another value, the then commands will not be executed, and bash shell will continue to execute the next command in the script.

Here is a simple example to explain this concept:

$ Cat if_then.sh

#! /Bin/baslif Test 1 = 2 then Echo "It worked" fi

At this time, running if_then will not print 'it worked'. If it is changed to the following, the echo "It worked" after then will be executed.

#! /Bin/baslif Test 1 = 1 then Echo "It worked" fi





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.