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

Step into shell-conditional testing and if statements (including script examples)

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

Share

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

content points

Condition test:

file test

integer test

String vs. Logic Test

If statement:

If single branch statement

If double branch statement

If multi-branch statement

I. Conditional test:

text command: tests whether a specific expression is true. When the condition is true, the return value of the test statement is 0, otherwise it is other values.

Format 1: test conditional expression Format 2:[conditional expression]

1. Document testing

file comparison

[operator file or directory]

Common test operator L:

-d: test whether it is a directory-e: test whether a directory or file exists-f: test whether it is a file-X: test whether the current user has execution permission-r: test whether the current user has read permission-W: test whether the current user has write permission

2, integer test:

Compare integer values:

Method 1: [integer 1 operator integer 2] Method 2: [$ (integer 1) operator integer 2] Method 3: [integer 1 operator integer 2]

Common test operators:

-eq: equal to-ne: not equal to-It: less than-gt: greater than-le: less than or equal to-ge: greater than or equal to

Test whether the file exists or not, if it does not exist, create it

Details of use:

3. String and logic test:

string comparison

Format 1:[String 1 = String 2 ] [String 1 != String 2 ] Format 2:[-z string]

Common test operators:

=: string content is the same! =: String content is different-z: String content is empty

Logic test:

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

Common logical test operators

-a or &&: logical and, meaning "and"-o or||: logical or, meaning "or"!: logical no

If statement:

if single branch

If double branching:

If multiple branches:

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