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

Introduction to the relational operator of linux shell

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

Share

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

This article will explain in detail about the linux shell relational operator introduction, Xiaobian think quite practical, so share to everyone as a reference, I hope you can have some harvest after reading this article.

numerical comparison operation

Relational operators only support numbers, not strings, unless the string value is a number. The following table lists common relational operators, assuming variable a is 10 and variable b is 20:

Examples:

#!/ bin/basha=10b=20if [ $a -eq $b ]then echo "$a -eq $b : a equals b"else echo "$a -eq $b: a does not equal b"fiif [ $a -ne $b ]then echo "$a -ne $b: a does not equal b"else echo "$a -ne $b : a equals b"fiif [ $a -gt $b ]then echo "$a -gt $b: a is greater than b"else echo "$a -gt $b: a is not greater than b"fiif [ $a -lt $b ]then echo "$a -lt $b: a less than b"else echo "$a -lt $b: a is not less than b"fiif [ $a -ge $b ]then echo "$a -ge $b: a greater than or equal to b"else echo "$a -ge $b: a less than b"fiif [ $a -le $b ]then echo "$a -le $b: a is less than or equal to b"else echo "$a -le $b: a is greater than b"fi

Run the script and the output is as follows:

10 -eq 20: a is not equal to b10 -ne 20: a is not equal to b10 -gt 20: a is not greater than b10 -lt 20: a is less than b10 -ge 20: a is less than b10 -le 20: a is less than or equal to b

Boolean operators

Examples:

#!/ bin/bash a=10 b=20 if [ $a != $b ] then echo "$a != $b : a is not equal to b" else echo "$a != $b: a equals b" fi if [ $a -lt 100 -a $b -gt 15 ] then echo "$a is less than 100 and $b is greater than 15 : returns true" else echo "$a is less than 100 and $b is greater than 15 : returns false" fi if [ $a -lt 100 -o $b -gt 100 ] then echo "$a less than 100 or $b greater than 100 : returns true" else echo "$a less than 100 or $b greater than 100 : returns false" fi if [ $a -lt 5 -o $b -gt 100 ] then echo "$a less than 5 or $b greater than 100 : returns true" else echo "$a less than 5 or $b greater than 100 : returns false" fi

Script Run:

10 != 20 : a is not equal to b10 less than 100 and 20 greater than 15 : returns true10 less than 100 or 20 greater than 100 : returns true10 less than 5 or 20 greater than 100 : returns false logical operator

The following describes the logical operators of Shell, assuming variable a is 10 and variable b is 20:

Examples:

#!/ bin/basha=10b=20if [[ $a -lt 100 && $b -gt 100 ]]then echo "returns true"else echo "returns false"fiif [[ $a -lt 100|| $b -gt 100 ]]then echo "returns true"else echo "returns false"fi

Run Results:

Returns false Returns true String operator

The following table lists common string operators, assuming variable a is "abc" and variable b is "efg":

Examples:

#!/ bin/basha="abc"b="efg"if [ $a = $b ]then echo "$a = $b : a equals b"else echo "$a = $b: a does not equal b"fiif [ $a != $b ]then echo "$a != $b : a does not equal b"else echo "$a != $b: a equals b"fiif [ -z $a ]then echo "-z $a : String length 0"else echo "-z $a : String length is not 0"fiif [ -n "$a" ]then echo "-n $a : String length is not 0"else echo "-n $a : string length 0"fiif [ $a ]then echo "$a : string is not empty"else echo "$a : empty string"fi

Run Results:

abc = efg: a is not equal to bbc!= efg : a is not equal to b-z abc : string length is not 0-n abc : string length is not 0abc : string is not empty

About the linux shell relational operators introduced to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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: 205

*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