The "shell" Linux shell if statement is detailed

Source: Internet
Author: User
Tags echo b

If statement 1.1 if statement explanation

If is a judgment statement, if the action is similar to [], the general judgment is more or the execution of the statement is more, then will use if

1.2 If format

First format

if [ 判断条件 ];then    内容else    内容fi

Second form, multiple judgments

if [ 判断条件 ];then    内容elif [ 判断条件 ];then    内容else    内容fi
1.3 Precautions

There must be spaces on both sides of [] after the IF statement

1.4 If example

1.if judgment
Determine if/root/a.txt exists, if present, echo 0, if not present, Echo1

#!/bin/bash -if [ -f /root/a.txt ];then        echo 0else        echo 1fi

2.if...elif...else ...
Determine if/root/a.txt exists, if present, then echo A.txt, determine if/root/b.txt exists, if present, then echo B.txt, if a.txt and b.txt do not exist, then output error

#!/bin/bash -if [ -f /root/a.txt ];then        echo "a.txt"elif [ -f /root/b.txt ];then        echo "b.txt"else        echo "error"fi

The "shell" Linux shell if statement is detailed

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.