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

How to use if statements in Linux shell script programming

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use Linux shell script programming if statements, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

If statement format

If condition

Then

Command

Else

Command

Fi, don't forget the ending.

The If statement forgot to end the fi.

Three conditional expressions of test.sh: line 14: syntax error: unexpected end of fi if

If

Command

Then if

Function

Then

If the command is executed successfully, it will return 0 (for example, grep, find a match)

Execution failed with non-0 returned (grep, no match found)

If [expression_r_r_r]

If the result of the then expression is true, it returns 0 # if the value of 0 leads to then.

If test expression_r_r_r

If the result of the then expression is false, return non-zero if to lead the non-zero value to then

[] & &-- Quick if

[- f "/ etc/shadow"] & & echo "This computer uses shadow passwors"

& & can be understood as then

If the expression on the left is true, the functional difference between the if of shell and the c language if executes the statement on the right

Shell if c language if

0 is true, take then on the contrary, non-0 take then

Direct if of integer variables is not supported

Must: if [I-ne 0] but support string variable direct if

If [str] if the string is not 0

Support variable direct if

If (I)

Use multiple command or functions as if conditions

The code is as follows:

Echo-n "input:"

Read user if

Multiple instructions, between which are equivalent to "and" (and)

Grep $user / etc/passwd > / tmp/null

Who-u | grep $user

All the instructions on the then were executed successfully, with a return value of $? If 0 is 0, 0 is true, run then

Echo "$user has logged"

Else instruction execution failed, $? Is 1, run else

Echo "$user has not logged"

Fi

# sh test.sh

Input: macg

Macg pts/0 May 15 15:55. 2075 (192.168.1.100)

Macg has logged

# sh test.sh

Input: ddd

Ddd has not logged takes the function as the if condition (the function is equivalent to command, and the advantage of the function is that its resume value can be customized)

If

Using function as if condition

Getyn

The value of then function then 0 is true.

Echo "your answer is yes"

If the value of else function is not 0, it is false to use else.

Echo "your anser is no"

Fi if command is equivalent to command+if $?

$vi testsh.sh

#! / bin/sh if

Cat 111-tmp.txt | grep ting1

Then

Echo found

Else

Echo "no found"

Fi

$vi testsh.sh

#! / bin/sh cat 111-tmp.txt | grep ting1

If [$?-eq 0]

Then

Echo $?

Echo found

Else

Echo $?

Echo "no found"

Fi

$sh testsh.sh

No found $sh testsh.sh

one

No found

$vi 111-tmp.txt

That is 222file

Thisting1 is 111file $sh testsh.sh

Thisting1 is 111file

Found

$vi 111-tmp.txt

That is 222file

Thisting1 is 111file $sh testsh.sh

Thisting1 is 111file

0

Found

Traditional if from sentence-- using conditional expression as if condition

The code is as follows:

If [conditional expression]

Then

Command

Command

Command

Else

Command

Command

Fi

Conditional expression

If [- f file] if the file exists

If [- d...] if the directory exists

If [- s file] if the file exists and is not empty

If [- r file] if the file exists and is readable

If [- w file] if the file exists and is writable

If [- x file] if the file exists and is executable

If [int1-eq int2] if int1 equals int2

If [int1-ne int2] if not equal to

If [int1-ge int2] if > =

If [int1-gt int2] if >

If [int1-le int2] if and <, will be treated as angle brackets, only-ge,-gt,-le,lt

[macg@machome ~] $vi test.sh echo "input a:"

Read a

If [$a-ge 100]; then

Echo 3bit

Else

Echo 2bit

Fi

[macg@machome ~] $sh test.sh

Input a:

one hundred and twenty three

3bit

[macg@machome ~] $sh test.sh

Input a:

twenty

2bit integer operator symbol-ge,-gt,-le,-lt, don't forget to add-

If test $a ge 100; then [macg@machome ~] $sh test.sh

Test.sh: line 4: test: ge: binary operator expected

If test $a-ge 100; then [macg@machome ~] $sh test.sh

Input a:

one hundred and twenty three

3bit

Logic is not! The opposite of a conditional expression

If [! Expression]

If [!-d $num] if the directory $num does not exist

The juxtaposition of logic and-a conditional expression

If [expression 1-an expression 2]

Or of a logical or-o conditional expression

If [expression 1-o expression 2]

Logical expression

The expression is used in conjunction with the preceding =! =-d-f-x-ne-eq-lt, etc.

Logical symbols are normally connected to other expressions without any parentheses (), which are juxtaposed.

If [- z "$JHHOME"-a-d $HOME/$num]

Note that logic is easily confused with-an and logic or-o with the operands of other strings or files

The most common form of assignment, where variables on both sides of = are evaluated before assignment.

Whether the left test variable is empty, and whether the right test directory (value) exists (whether the value is valid).

The code is as follows:

[macg@mac-home ~] $vi test.sh

:

Echo "input the num:"

Read num

Echo "input is $num" if [- z "$JHHOME"-a-d $HOME/$num] if the variable $JHHOME is empty and the $HOME/$num directory exists

Then

JHHOME=$HOME/$num is assigned.

Fi

Echo "JHHOME is $JHHOME"

[macg@mac-home ~] $sh test.sh

Input the num:

Input is ppp

The JHHOME is directory-d $HOME/$num does not exist, so $JHHOME is not assigned by then

[macg@mac-home ~] $mkdir ppp

[macg@mac-home ~] $sh test.sh

Input the num:

Input is ppp

JHHOME is / home/macg/ppp is an example of-o, but it reveals the problem that "=" must leave spaces on both sides.

Echo "input your choice:"

Read ANS if [$ANS= "Yes"-o $ANS= "yes"-o $ANS= "y"-o $ANS= "Y"]

Then

ANS= "y"

Else

ANS= "n"

Fi

Echo $ANS

[macg@machome ~] $sh test.sh

Input your choice:

Y

[macg@machome ~] $sh test.sh

Input your choice:

No

Y

Why the input is not yes, the result is still y (go then)

Because = is concatenated and becomes the variable $ANS= "Yes", and the variable is empty, so go else.

[macg@machome ~] $vi test.sh echo "input your choice:"

Read ANS echo "input your choice:"

Read ANS

If [$ANS = "Yes"-o $ANS = "yes"-o $ANS = "y"-o $ANS = "Y"]

Then

ANS= "y"

Else

ANS= "n"

Fi

Echo $ANS

[macg@machome ~] $sh test.sh

Input your choice:

No

[macg@machome ~] $sh test.sh

Input your choice:

Yes

Y

[macg@machome ~] $sh test.sh

Input your choice:

Y

Y = take test conditional expression as if condition = =

If test $num-eq 0 is equivalent to if [$num-eq 0]

Test expression, no []

If test $num-eq 0

Then

Echo "try again"

Else

Echo "good"

Fi

Man test

[macg@machome ~] $man test

[(1) User Commands [(1) SYNOPSIS

Test EXPRESSION

[EXPRESSION]

[- n] STRING

Both the length of STRING is nonzero-n and direct $str are non-zero conditions.

-z STRING

The length of STRING is zero

STRING1 = STRING2

The strings are equal

STRING1! = STRING2

The strings are not equal

INTEGER1-eq INTEGER2

INTEGER1 is equal to INTEGER2

INTEGER1-ge INTEGER2

INTEGER1 is greater than or equal to INTEGER2

INTEGER1-gt INTEGER2

INTEGER1 is greater than INTEGER2

INTEGER1-le INTEGER2

INTEGER1 is less than or equal to INTEGER2

INTEGER1-lt INTEGER2

INTEGER1 is less than INTEGER2

INTEGER1-ne INTEGER2

INTEGER1 is not equal to INTEGER2

FILE1-nt FILE2

FILE1 is newer (modification date) than FILE2

FILE1-ot FILE2

FILE1 is older than FILE2

-b FILE

FILE exists and is block special

-c FILE

FILE exists and is character special

-d FILE

FILE exists and is a directory

-e FILE

FILE exists file exists

-f FILE

FILE exists and is a regular file file exists and is a normal file

-h FILE

FILE exists and is a symbolic link (same as-L)

-L FILE

FILE exists and is a symbolic link (same as-h)

-G FILE

FILE exists and is owned by the effective group ID

-O FILE

FILE exists and is owned by the effective user ID

-p FILE

FILE exists and is a named pipe

-s FILE

FILE exists and has a size greater than zero

-S FILE

FILE exists and is a socket

-w FILE

FILE exists and is writable

-x FILE

FILE exists and is executable

The most commonly used simplified if statement

& & if it is "front", then "back"

The code is as follows:

[- f / var/run/dhcpd.pid] & & rm / var/run/dhcpd.pid check whether the file exists and delete it if it exists

| | if it is not "front", then the back

The code is as follows:

[- f / usr/sbin/dhcpd] | | exit 0 verifies whether the file exists, and exits if it exists

Check the parameters with simplified if and $1 "2J" 3, and call help if it is unreasonable.

The code is as follows:

[- z "$1"] & & help if the first parameter does not exist (the-z string length is 0)

["$1" = "- h"] & & help displays help if the first parameter is-h

Examples

The code is as follows:

#! / bin/sh

[- f "/ etc/sysconfig/network-scripts/ifcfg-eth0"] & & rm-f / etc/sysconfig/network-scripts/ifcfg-eth0

Cp ifcfg-eth0.bridge / etc/sysconfig/network-scripts/ifcfg-eth0

[- f "/ etc/sysconfig/network-scripts/ifcfg-eth2"] & & rm-f / etc/sysconfig/network-scripts/ifcfg-eth2

Cp ifcfg-eth2.bridge / etc/sysconfig/network-scripts/ifcfg-eth2

[- f "/ etc/sysconfig/network-scripts/ifcfg-eth0:1"] & & rm-f / etc/sysconfig/network-scripts/ifcfg-eth0:1

These are all the contents of this article entitled "how to use if statements in Linux shell scripting programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report