Shell conditional Judgment statement and if

Source: Internet
Author: User
Tags chmod

First, conditional judgment statement

Meet the criteria to execute the corresponding command

[Condition]: more commonly used in scripts

Cases:

[-f/etc/hosts]

Test condition: More commonly used for Linux character terminals

1. File or directory judgment

-F: Determines whether the file is

-D: Determine if the directory

-E: Determine if there is

-R: Determine if the file/directory has Read permissions

-W: Determines whether the file/directory has write permissions

-X: Determines whether the file/directory has execute permissions

Cases:

[-f/etc/hosts]//Determine if/etc/hosts is a file, as a file, then echo $? must be 0.

[!-f/etc/hosts]; Touch/etc/hosts//Determine if/etc/hosts is a file, such as not a file, echo $? must be 0.

[-D/ETC]//To determine if/etc is a directory, as a directory, then echo $? must be 0.

[-w/etc/hosts]//To determine if/etc/hosts has write permission, such as Write permission, then echo $? must be 0

[-e/boot]//Determine if/boot exists, if present, then echo $? must be 0

2. Numerical comparison

-EQ: Determine whether the values on both sides are equal

-ne: Judging whether the values on both sides are unequal

-GT: Determine if the left value is greater than the right value

-ge: Determine if the left value is greater than or equal to the right value

-LT: Determine if the left value is less than the right value

-le: Determine if the left value is less than or equal to the right value

Cases:

[1-eq 1]//judgment 1 is equal to 1, if Equal then echo $? must be 0

[3-le 5]//judgment 3 is less than or equal to 5, such as less than equals the echo $? Yes 0

Note: Numeric comparisons can only compare integers

3. String comparisons

= =: Determine whether the strings are exactly equal on both sides

-Z: Determine if the value of the required variable is not empty

Cases:

[$PWD = =/root]//Determine if the PWD variable value equals/root, if equal to echo $? must be 0.

[! $PWD = =/root]//Determine if the PWD variable value equals/root, if not equal to echo $? definitely 0.

[-Z $PWD]//Determine if the PWD variable value is empty, or if it is empty, echo $? must be 0.

Note: string refers to letters, numbers, Chinese

4. Logic Test

&& or; Or-A//and relationship, the condition is set up to continue execution; make && make install (continuation of subsequent commands after successful execution); Find/-name A *-a-type f

|| OR-o//or relationship, which satisfies any condition to continue execution; find/-name A *-o-type f

! Conditional inversion

Second, if statement

Single BRANCH statement: satisfies the condition, executes the following command; command effect with [-f/etc/a] && chmod +x/etc/a

If [conditional judgment statement]

Then

Command

Fi

Cases:

if [-f/etc/a]

Then

chmod +x/etc/a

Fi

Two-branch statement: satisfies the condition, then executes the command one, if does not satisfy the condition, executes the command two

If [conditional judgment statement]

Then

Command One

Else

Command two

Fi

Multi-branch statement: satisfies the judgment one executes the command, if does not satisfy then continues to judge two, satisfies then executes the command two, XXXX, if does not satisfy then executes the command x

If [conditional judgment statement I]

Then

Command One

elif [Conditional Judgment Statement II]

Then

Command two

elif [Conditional Judgment Statement III]

Then

Command three

Else

Command x

Fi

#!/bin/bash

Read-p "Please enter your score: [1-100]" A

If [$A-le 100]

Then

If [$A-eq 100]

Then

echo "优"

elif [$A-le] && [$A-ge 70]

Then

echo "良"

elif [$A-le] && [$A-ge 60]

Then

echo "及格"

Else

echo "你退群吧!!!"

Fi

Else

Echo, "You can not do it."

Fi

Note: If first determine whether the user input value within 100, such as within 100 to continue to judge, if not the return "You can not do"

Shell condition judgment statement and if

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.