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

The method of using if statement in Shell script in Linux system

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

Share

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

This article mainly explains "the method of using if sentence in Shell script in Linux system". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "the method of using if sentence in Shell script in Linux system".

Bourne Shell's if statement is the same as most programming languages-check whether the condition is true, if the condition is true, shell will execute the code block specified by the if statement, if the condition is false, shell will skip the if code block and continue to execute the later code.

The syntax of the if statement:

The code is as follows:

If [judgment condition]

Then

Command1

Command2

…… ..

Last_command

Fi

Example:

#! / bin/bash

Number=150

If [$number-eq 150]

Then

Echo "Number is 150"

Fi

If-else statement:

In addition to the standard if statement, we can also add else code blocks to extend the if statement. The main purpose of this is to execute the code block in the if statement if the if condition is true and the code block in the else statement if the if condition is false.

Syntax:

The code is as follows:

If [judgment condition]

Then

Command1

Command2

…… ..

Last_command

Else

Command1

Command2

…… ..

Last_command

Fi

Example:

The code is as follows:

#! / bin/bash

Number=150

If [$number-gt 250]

Then

Echo "Number is greater"

Else

Echo "Number is smaller"

Fi

If..elif..else..fi statement (abbreviated else if)

In Bourne Shell's if statement syntax, the block of code in the else statement is executed when the if condition is false. We can also nest if statements together to achieve the detection of multiple conditions. We can use the elif statement (short for else if) to build the detection of multiple conditions.

Syntax:

The code is as follows:

If [judgment condition 1]

Then

Command1

Command2

…… ..

Last_command

Elif [judgment condition 2]

Then

Command1

Command2

…… ..

Last_command

Else

Command1

Command2

…… ..

Last_command

Fi

Example:

The code is as follows:

#! / bin/bash

Number=150

If [$number-gt 300]

Then

Echo "Number is greater"

Elif [$number-lt 300]

Then

Echo "Number is Smaller"

Else

Echo "Number is equal to actual value"

Fi

Multiple if statements:

If and else statements can be nested within a bash script. The keyword "fi" indicates the end of the inner if statement, and all if statements must end with the keyword "fi".

The nested syntax of the basic if statement:

The code is as follows:

If [judgment condition 1]

Then

Command1

Command2

…… ..

Last_command

Else

If [judgment condition 2]

Then

Command1

Command2

…… ..

Last_command

Else

Command1

Command2

…… ..

Last_command

Fi

Fi

Example:

The code is as follows:

#! / bin/bash

Number=150

If [$number-eq 150]

Then

Echo "Number is 150"

Else

If [$number-gt 150]

Then

Echo "Number is greater"

Else

Echo "'Number is smaller"

Fi

Fi

Thank you for reading, the above is the content of "the method of using if statement in Shell script in Linux system". After the study of this article, I believe you have a deeper understanding of the method of using if statement in Shell script in Linux system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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