shell比较运算与if逻辑判断

2021/5/30 7:25:43

本文主要是介绍shell比较运算与if逻辑判断,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、shell比较运算

(1)数学比较运算(只能比较整数,小数请放大)

在shell中没有>、<、>=等操作符,想要表示上述关系需要用到以下运算符

-eq		等于
-gt		大于
-lt		小于
-ge		大于等于
-le		小于等于
-ne		不等于

在shell中,0表示为真,非0值为假,和C语言中相反

下面使用test指令来示例说明:

[root@localhost ~]# test 1 -eq 1; echo $?	#test指令用于测试一个表达式的真假,通过返回值返回,$?查看上一个命令的返回值
0
[root@localhost ~]# test 1 -gt 1; echo $?
1
[root@localhost ~]# test 3 -gt 2; echo $?
0
[root@localhost ~]# test 3 -ge 2; echo $?
0
[root@localhost ~]#

在shell中小数比较运算

#!/bin/bash
# 示例:比较2.4是否大于1.5

var1=`echo "2.4*10"|bc|cut -d "." -f1`  #cut命令以"."为分隔符取第一个字段
echo "var1=$var1"       #用于测试
var2=`echo "1.5*10"|bc|cut -d "." -f1`
echo "var2=$var2"       #用于测试
test $var1 -gt $var2
echo $?

# 运行结果如下

var1=24
var2=15
0

(2)字符串比较运算

#运算符解释,注意字符串一定别忘了使用引号引起来
    ==          等于   
    !=          不等于
    -n          检查字符串的长度是否大于0  
    -z          检查字符串的长度是否为0
# 示例
root@wkr:~# test "hello" == "hello";echo $?
0
root@wkr:~# test "hello" != "hello";echo $?
1
root@wkr:~# test -n "222" ; echo $?
0
root@wkr:~# test -z "" ; echo $?
0 
root@wkr:~# test $USER=="root" ; echo $?	#判断当前用户是否是root用户
0

(3)文件比较与检查

test File1 –ef File2    两个文件是否为同一个文件,可用于硬连接。主要判断两个文件是否指向同一个inode。
test File1 –nt File2    判断文件1是否比文件2新
test File1 –ot File2    判断文件1比是否文件2旧
test –b file   #文件是否块设备文件
test –c File   #文件是否是字符设备文件
test –d File   #文件是否是目录
test –e File   #文件是否存在 (常用)
test –f File   #文件是否为正规文件 (常用)
test –g File   #文件是否是设置了组id
test –G File   #文件属于的有效组ID
test –h File   #文件是否是一个符号链接(同-L)
test –k File   #文件是否设置了Sticky bit位
test –b File   #文件存在并且是块设备文件
test –L File   #文件是否是一个符号链接(同-h)
test –o File   #文件的属于有效用户ID
test –p File   #文件是一个命名管道
test –r File   #文件是否可读
test –s File   #文件是否是非空白文件
test –t FD     #文件描述符是在一个终端打开的
test –u File   #文件存在并且设置了它的set-user-id位
test –w File   #文件是否存在并可写
test –x File   #文件属否存在并可执行

(4)逻辑运算符

&&	#逻辑与
||	#逻辑或
!	#逻辑非

示例: 结合下面的if判断某一个文件是否存在

[root@localhost shell]# pwd
/root/shell
[root@localhost shell]# vim file.sh
[root@localhost shell]# ls
compare.sh  elseif.sh  file.sh  if.sh  kfc.sh  ping.sh  type.sh		#当前路径下无aa.txt文件
[root@localhost shell]# bash file.sh		#执行脚本
aa.txt文件不存在
[root@localhost shell]# ls					#没有则创建aa.txt文件
aa.txt  compare.sh  elseif.sh  file.sh  if.sh  kfc.sh  ping.sh  type.sh
[root@localhost shell]# bash file.sh		#再次执行脚本aa.txt已经有
aa.txt文件存在
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
file.sh
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
if [ ! -e /root/shell/aa.txt ]			#判断/root/shell/aa.txt文件是否存在
    then
    echo "aa.txt文件不存在"
    touch aa.txt
else
    echo "aa.txt文件存在"
fi

2、if判断

(1)单if判断

语法格式

if [ 条件 ]			#注意条件两边的空格必须有
	then
		command1
		commandn
fi

示例

#!/bin/bash
#判断当前用户是不是root用户
if [ $USER=root ]
	then
	echo "当前用户为root用户"
fi

(2)if-else判断

语法格式

if [ 条件 ]
	then
	command1
else
	command2
fi

示例:下面是一个模仿ping的示例

$1表示命令行传递的第一个参数

$2表示命令行传递的第二个参数

#!/bin/bash

ping $1 -c 3 >> /dev/null  #ping3次,将ping的结果重定向到/dev/null

if [ $? -eq 0 ]
    then
    echo -e "ping $1 \033[40;32m SUCCESS \033[0m";
else
    echo -e "ping $1 \033[40;31m FAILED \033[0m";

fi

在这里插入图片描述

(3)if-elif-else

语法格式

if [ 条件 ]
	then
	command
elif [ 条件 ]
	then
	command
else
	command
if

**示例:**比较两个数的大小

#!/bin/bash

if [ $1 -eq $2 ]
    then
    echo "$1==$2"
elif [ $1 -gt $2 ]
    then
    echo "$1>$2"
else
    echo "$1<$2"
fi

在这里插入图片描述

(4)多层if嵌套

if [ 条件 ]
	then
	command
	if [ 条件 ]
		then
		command
	else
		command
	fi
else
	command
fi

3、if高级用法

(1)条件符号使用双圆括号,可以在条件中植入表达式

#!/bin/bash
if (( (5+1)*2 < 12 )) 				#双括号里面的表达式只能是整数运算
    then
        echo "yes"
else
        echo "no"
fi

(2)使用[[]]双方括号,可在条件中支持逻辑运算与正则

逻辑运算

#!/bin/bash

str1=""
str2="hello"

if [[ -z $str1 || -z $str2 ]]		#-z判断字符串长度是否为0
    then
        echo "str1 str2 至少有一个为空"
else
        echo "str1 str2 都不为空"
fi
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@localhost shell]# bash z.sh
str1 str2 至少有一个为空

正则表达式:使用方法[[ str =~ regex ]]

#!/bin/bash
for var in abc abbc bbc ac cad a+c
    do
        if [[ $var =~ ^a.*c$ ]]
            then
                echo "$var"
        fi
done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@localhost shell]# bash reg.sh
abc
abbc
ac
a+c

(3)简写if

省略if关键词,条件为真用&&符链接命令块,条件为假用||链接命令块

#!/bin/bash

#全写
if [ $USER == ROOT ]
    then
        echo "root用户"
else
        echo "$USER用户"
fi

#简写
[[ $USER == ROOT ]] && echo "root用户" || echo "$USER用户"

[root@localhost shell]# bash root.sh
root用户
root用户



这篇关于shell比较运算与if逻辑判断的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程