bash shell逻辑表达式的使用(shell 数值运算)万万没想到

随心笔谈12个月前发布 admin
108 0



目录前言test数字比较字符串比较文件比较、类型/属性检查逻辑与:-a,逻辑或:-o[命令[[ expression ]]

控制语句需要逻辑表达式进行分支判断,bash shell逻辑表达式一般有3种写法:、 、。

test命令的使用格式是:test expression。expression就是我们的逻辑表达式,如果值为true,执行这条命令的返回值为0,false则非0。示例:

#! /bin/bash
#1==1
test 1 -eq 1
echo “1==1 : $?”
#1==2
test 1 -eq 2
echo “1==2 : $?”

执行结果:

注意,test命令对于值的比较有固定的写法(毕竟是个命令),主要分为3种:数值比较、字符串比较、文件类型检查。

数字的比较不能用==、!=、<、>这些符号比较,有固定的参数,就像上面的示例一样,1==1,要写成1 -eq 1,下面作个说明:

p.s. 如果记不住,可以用man test查看,下面是执行man test中关于数值比较的说明部分:

       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

使用示例:test integer1 -eq integer2:比较integer1等于integer2,其它例同

字符串比较,可以用=或!=这些符号,另外还有一些非空检查的选项,如下从(man test就看到了)复制过来的,加了点说明:

       -n STRING :字符串的长度大于0

the length of STRING is nonzero

       -z STRING:字符串的长度为0

the length of STRING is zero

上面的2项,可以对字符串是否为空字符串判断,如果字符串为空(不是空字符串),无论是-n还是-z都是true

       STRING1=STRING2

the strings are equal

       STRING1 !=STRING2

the strings are not equal

示例:

#! /bin/bash
str1=”str1″
test -n $str1;echo $?
test -z $str2;echo $?
test -n $str2;echo $?
test x$str2=x;echo $?
test $str1 !=””;echo $?

执行结果(都是true):

下面就不过多解释了,每个选项的描述很清楚了,也不需要刻意去记每个选项,还是那句话:man test,

       FILE1 -ef FILE2

FILE1 and FILE2 have the same device and inode numbers

       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

       -f FILE

FILE exists and is a regular file

-g FILE

FILE exists and is set-group-ID

       -G FILE

FILE exists and is owned by the effective group ID

       -h FILE

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

       -k FILE

FILE exists and has its sticky bit set

       -L FILE

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

       -O FILE

FILE exists and is owned by the effective user ID

       -p FILE

FILE exists and is a named pipe

       -r FILE

FILE exists and read permission is granted

       -s FILE

FILE exists and has a size greater than zero

       -S FILE

FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE

FILE exists and its set-user-ID bit is set

       -w FILE

FILE exists and write permission is granted

       -x FILE

FILE exists and execute (or search) permission is granted

示例:

#! /bin/bash
#文件是否存在
test -e demo.sh
echo “demo.sh exists: $?”
test -e demo1.sh
echo “demo1.sh exists: $?”

执行结果:

因为test是个命令,所以逻辑与/或不能像其它语言一样使用&&、||等符号,这几个符号在shell中可是有特殊的意义,比如上面示例,用&&连接两条命令,每1条执行成功,才会执行下一条。

示例:

#! /bin/bash
#1等于1并且1等于2
test 1 -eq 1 -a 1 -eq 2
echo “1==1 && 1==2: $?”
#1等于1或者1等于2
test 1 -eq 1 -o 1 -eq 2
echo “1==1 || 1==2: $?”

执行结果:

注意是,这是个命令,写法一般是[ expression ],注意expression前后的空格,“[”可不是个符号, 是个命令,等同于test的命令,所以它的expression,和test的用法一样。

这里重点是是个命令,所以这两个符号和表达式之前要注意空格,另外,既然是和test一样的命令,逻辑与不能&符号连接,也要用-a等连接:

#! /bin/bash
#1等于1并且1等于2
[ 1 -eq 1 -a 1 -eq 1 ]
echo “1==1 && 1==2: $?”
#1等于1或者1等于2
[ 1 -eq 1 -o 1 -eq 1 ]
echo “1==1 || 1==2: $?”

执行结果:

虽然””只比,多了一个,但是含义却是不一样的,””是一个命令,而””却是一个关键字。

不过的功能却比要强大不少,是的增强版。比如可以用&&作为逻辑与连接符而不支持用-a,同理逻辑或是||,但不支持-o。除此,其它的基本都兼容,另外比,还有一些其它能力的支持,比如使用<,>等这些比较数字,但是因为””是个命令,所以并不支持(需要转义)。

所以,在感觉上来说使用更符合编程习惯,如下:

#! /bin/bash
#数字计算比较,
[[ 5 < $((3+3)) && 4 >3 ]]
echo “[[ 5 < 3+3 && 4 >3 ]] : $?”
#使用[],就不支持,运行到这里就报错了
[ 5 < $((3+3)) && 4 >3 ]

执行结果:

可以看到第7行代码执行不通过,所以用的写法就该是这样:

到此这篇关于bash shell逻辑表达式的使用的文章就介绍到这了,更多相关shell逻辑表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:linux shell 逻辑运算符、逻辑表达式详细介绍linux shell中 if else以及大于、小于、等于逻辑表达式介绍

© 版权声明

相关文章