bash条件测试及变量比较

![endif]-->!-->![endif]-->!-->![endif]-->!-->

bash条件测试及变量比较

一、     条件表达式

1. 基本的if语句

if [ … ]

then

….

Fi

 

2. 多个if语句

if […]

then

Elif […]

Then

Fi

 

3. 嵌套if语句

If […]

Then

   If[…]

   Then

  

   Fi

Else

fi

 

二、     字符串和数字比较

 

Number

String

大于

-gt

/>

小于

-lt

/<

等于

-eq

==

大于等于

-ge

Single[] doesnot support

小于等于

-le

Single[] doesnot support

不等于

-ne

=

 

三、      关键点

1.      ifelif后面有then

2.      else if是非法的

3.      单个中括号[]里面的”<”“ >”需要转义,且不支持”>=” “<=”

4.      “z” 测试字符串是否为NULL

5.      “n” 测试字符串是否不为NULL

 

四、     代码

#! /bin/bash

echo "test you"

a=12

b=13

 

# number compare

# -eq: equal

# -ne: not equal

# -gt: great

# -ge: great and equal

# -lt: little

# -le: little and equal

if [ "$a" -ne "$b" ]

then

echo "$a is not equel to $b"

fi

 

if [ "$a" -eq "$b" ]

then

echo "$a is equal to $b"

else

echo "$a is not equal to $b"

fi

 

if [ "$a" -eq "$b" ]

then

echo "$a is equal to $b"

elif [ "$a" -lt "$b" ]

then

echo "$a is little to $b"

elif [ "$a" -gt "$b" ]

then

echo "$a is great to $b"

else

echo "Default"

fi

#########Key Point##############

#########1."then" after "if" or "elif"############

#########2."elif" is ok, not "else if"############

 

c=abc

d=abd

e=

#string compare

 

if [ "$c" == "$d" ]

then

echo "string $c is equal to $d"

elif [ "$c" /> "$d" ]

then

echo "string $c is great to $d"

elif [ "$c" /< "$d" ]

then

echo "string $c is little to $d"

elif [ "$c" != "$d" ]

then

echo "string $c is not equal to $d"

fi

 

if [ -z "$e" ]

then

echo " $e is NULL"

else

echo " $e is not NULL"

fi

 

if [ -n "$e" ]

then

echo "$e is not NULL"

else

echo "$e is NULL"

fi

###########KEy point#################

###########1.For string comparing, ">" "<"need escape symbol

###########2."z": zero is used to test whether the string is NULL

###########3."n": not zero is used to test whether the string is not NULL

 

 

f=8

if [ "$f" -gt 0 ]

then

echo "f is positive"

   if [ "$f" -gt 5 ]

   then

   echo "f is a big positive"

   else

   echo "f is a small positive"

   fi

else

echo "f is negative"

   if [ "$f" -gt -5 ]

   then

   echo "f is a big negative"

   else

   echo "f is a small negative"

   fi

fi

 

 

 

 

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
发布了48 篇原创文章 · 获赞 36 · 访问量 34万+

![endif]-->!-->![endif]-->!-->![endif]-->!-->

bash条件测试及变量比较

一、     条件表达式

1. 基本的if语句

if [ … ]

then

….

Fi

 

2. 多个if语句

if […]

then

Elif […]

Then

Fi

 

3. 嵌套if语句

If […]

Then

   If[…]

   Then

  

   Fi

Else

fi

 

二、     字符串和数字比较

 

Number

String

大于

-gt

/>

小于

-lt

/<

等于

-eq

==

大于等于

-ge

Single[] doesnot support

小于等于

-le

Single[] doesnot support

不等于

-ne

=

 

三、      关键点

1.      ifelif后面有then

2.      else if是非法的

3.      单个中括号[]里面的”<”“ >”需要转义,且不支持”>=” “<=”

4.      “z” 测试字符串是否为NULL

5.      “n” 测试字符串是否不为NULL

 

四、     代码

#! /bin/bash

echo "test you"

a=12

b=13

 

# number compare

# -eq: equal

# -ne: not equal

# -gt: great

# -ge: great and equal

# -lt: little

# -le: little and equal

if [ "$a" -ne "$b" ]

then

echo "$a is not equel to $b"

fi

 

if [ "$a" -eq "$b" ]

then

echo "$a is equal to $b"

else

echo "$a is not equal to $b"

fi

 

if [ "$a" -eq "$b" ]

then

echo "$a is equal to $b"

elif [ "$a" -lt "$b" ]

then

echo "$a is little to $b"

elif [ "$a" -gt "$b" ]

then

echo "$a is great to $b"

else

echo "Default"

fi

#########Key Point##############

#########1."then" after "if" or "elif"############

#########2."elif" is ok, not "else if"############

 

c=abc

d=abd

e=

#string compare

 

if [ "$c" == "$d" ]

then

echo "string $c is equal to $d"

elif [ "$c" /> "$d" ]

then

echo "string $c is great to $d"

elif [ "$c" /< "$d" ]

then

echo "string $c is little to $d"

elif [ "$c" != "$d" ]

then

echo "string $c is not equal to $d"

fi

 

if [ -z "$e" ]

then

echo " $e is NULL"

else

echo " $e is not NULL"

fi

 

if [ -n "$e" ]

then

echo "$e is not NULL"

else

echo "$e is NULL"

fi

###########KEy point#################

###########1.For string comparing, ">" "<"need escape symbol

###########2."z": zero is used to test whether the string is NULL

###########3."n": not zero is used to test whether the string is not NULL

 

 

f=8

if [ "$f" -gt 0 ]

then

echo "f is positive"

   if [ "$f" -gt 5 ]

   then

   echo "f is a big positive"

   else

   echo "f is a small positive"

   fi

else

echo "f is negative"

   if [ "$f" -gt -5 ]

   then

   echo "f is a big negative"

   else

   echo "f is a small negative"

   fi

fi

 

 

 

 

猜你喜欢

转载自blog.csdn.net/qq_34924407/article/details/85332650