Shell-03-表达式和运算符

2021/8/6 7:10:00

本文主要是介绍Shell-03-表达式和运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!




表达式和运算符

条件表达式语句

[ 1 -eq 1 ]

[[ 1 -eq 1 ]]

test 1 -eq 1  等同于  [  1 -eq 1 ]

[root@satest_192-168-3-121 shell]# [ 1 -eq 1 ]
[root@satest_192-168-3-121 shell]# echo $?
0
[root@satest_192-168-3-121 shell]#
[root@satest_192-168-3-121 shell]# [[ 1 -eq 1 ]]
[root@satest_192-168-3-121 shell]# echo $?
0
[root@satest_192-168-3-121 shell]#
[root@satest_192-168-3-121 shell]# test 1 -eq 1
[root@satest_192-168-3-121 shell]# echo $?
0
[root@satest_192-168-3-121 shell]#


整数比较

image-20210805114042799



字符串比较

image-20210805114139491

注意:
使用 -z 或者 -n 判断字符串长度时,需要加双引号
使用 [ ... ] 时 需要给变量值加上双引号,不然判断会出错
使用 [[ ... ]] 时,则不需要

image-20210805114232122



文件测试

image-20210805114821416



布尔运算符

image-20210805114845648



逻辑判断符

image-20210805114928288



整数运算

image-20210805115005303



其他运算工具

image-20210805115030630


shell 不支持浮点数比较,可以借助bc来完成需求

image-20210805115051228

image-20210805115101360




shell括号用途总结

image-20210805115224106




这篇关于Shell-03-表达式和运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程