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 for shell programming

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

Share

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

shell conditional statement

I. Conditional testing

Test command:

Tests whether a specific 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] There should be at least one space between the conditional expression and the square brackets.

II. Document testing

[operator file or directory]

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

-r: Tests whether the current user has permission to read (Read)

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

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

III. Comparison of integer values

[integer 1 operator integer 2 ]

Common test operators

-eq =(Equal)

-ne: not equal

-gt: Greater Than

It is (less Than)

-le: Less or equal

-ge: Greater or Equal

IV. String Comparison

[String 1 = String 2 ]

[String 1 != String 2 ]

format 2

[ -z string]

Common test operators

=: String content identical

!=:String content is different,! The sign means the opposite.

-z: string content is empty

logic test

Format 1:[Expression 1 ] operator [Expression 2 ]

Format 2: Command 1 Operator Command 2

Common test operators

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

-o or|| logical OR logical OR

!:logical no

Unary operator: i=1

i++: i=$i+1 (i=++i first operation then assignment;i=i++ first assignment in operation)

binary operator

a+b=c

ternary operator

Conditions && Results 1|| results 2

Structure of the if statement

1. single branch structure

2. double branch structure

3. multibranched structure

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