Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Conditional statements of Shell scripts

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

condition test

1. Document testing

2, integer test

3. String and logic tests

If statement:

if single branch statement

if double branch statement

if multi-branch statement

if nested statement

Test command:

Tests whether a particular expression holds true. When the condition holds, the return value of the test statement is 0, otherwise it is another value.

Format 1: test conditional expression

Format 2:[conditional expression] //note spaces

file test

File testing refers to judging whether a file or directory corresponds to a given path name, or whether a file is readable, writable, executable, etc.

Common test operators:

-d: Test whether it is Directory

-e: Test whether the directory or file exists (Existing)

-f: Test whether it is a file (File)

-r: Test whether the current user has read permission

-w: Tests whether the current user has permission to write

-x: Tests whether the current user has permission to execute (eXcute)

Actual operation:

Format 1:

test -d /etc/sysconfig

echo $? (If the output value is 0, the condition is true)

Format 2:

[ -d /etc/sysconfig ]

echo $? (If the output value is 1, the condition is not true)

Compare integer values:

Format: [integer 1 operator integer 2 ]

Common test operators:

-eq =(Equal)

-ne: not equal

-gt: Greater Than

-lt: Lesser Than

-le: Less or Equal

-ge: Greater or Equal

string comparison

Format 1:[String 1 = String 2 ]

[String 1 ! = string 2 ]

Format 2:[ -z string]

Common test i operators:

=: String content identical

!=:String content is different,! expresses a contrary intention

-z: string content is empty

logic test

Format 1:[Expression 1 ] Operator [Expression 2 ]...

Format 2: Command 1 Operator Command 2...

Common operation options

-a or &&: logical and, meaning "and"

-o or|| logical OR logical OR

!:Logic no means "no."

If statement:

Single branch structure:

if conditional test operation

then

command sequence

fi

Double branch structure:

if conditional test operation

then

Command sequence 1

else

Command Sequence 2

fi

Multi-branch structure:

if conditional test operation

then

Command sequence 1

elif conditional test operation 2

then

Command Sequence 2

else

Command sequence 3

fi

If nested statements:

Objective: Run a race, enter the finals within 10 seconds, enter gender, prompt to enter men's group or women's group respectively, use multiple judgments, nested if to achieve

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report