Shell script Programming: Conditional judgment if Statement usage summary

Source: Internet
Author: User

Shell scripting, there are three kinds of control structure are: sequential structure, conditional judgment structure, loop structure. This article summarizes the use of conditional judgment constructs in shell scripts.

The structure of conditional judgment is divided into three kinds, single branch, double branch, multi Branch, and so on.

The syntax for a single branch structure is as follows:

if [expression]; then

Statement1

Statement2

.........

Fi


Two-branch syntax structure:

if [Expression];then

Statement1

Statement2

.....

Else

Statement3

Statement4

......

Fi


Multi-branch syntax structure:

if [Expresion1];then

Statement1

....

elif [Expression2];then

Statement2

......

elif [Expression3];then

Statement3

......

Else

Statement4

......

Fi

You need to be aware of when to use then, and then the semicolon in front of it is essential, otherwise it will go wrong.

If always paired with fi

If or elif, there must be then.


Multi-branch Conditional statement instance:

[email protected] sh]# cat test.sh
#!/bin/bash
Read-p ' Please input your file ' file
#FILE =/etc/passwddd
if [!-e $FILE];then
echo "$FILE is not exist"
Exit 3
Fi
If [-F $FILE];then
echo "$FILE is common FILE"
elif [-D $FILE];then
echo "$FILE is directory FILE"
elif [-X $FILE];then
echo "$FILE can be excuteable"
elif [-W $FILE];then
echo "$FILE can be written"
elif [-R $FILE];then
echo "$FILE can read"
Else
echo "Unkown file"
Fi


Shell script Programming: Conditional judgment if Statement usage summary

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.