linux 中 常用的条件判断语句
2022/5/6 7:15:22
本文主要是介绍linux 中 常用的条件判断语句,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
测试文件
root@PC1:/home/test2# touch file.txt root@PC1:/home/test2# mkdir data root@PC1:/home/test2# ls data file.txt root@PC1:/home/test2# ll -h total 12K drwxr-xr-x 3 root root 4.0K 5月 5 08:54 ./ drwxr-xr-x 10 root root 4.0K 5月 1 20:56 ../ drwxr-xr-x 2 root root 4.0K 5月 5 08:54 data/ ## 目录文件 -rw-r--r-- 1 root root 0 5月 5 08:53 file.txt ## 一般文件
1、判断是否为文件
root@PC1:/home/test2# [ -f file.txt ] root@PC1:/home/test2# echo $? ## 如果判断成功则$?变量为0 0 root@PC1:/home/test2# [ -f data ] root@PC1:/home/test2# echo $? ## 如果判断失败则$?变量为1 1
2、判断是否为目录
root@PC1:/home/test2# [ -d file.txt ] root@PC1:/home/test2# echo $? ## 判断失败 1 root@PC1:/home/test2# [ -d data ] root@PC1:/home/test2# echo $? ## 判断成功 0
3、判断文件是否存在
root@PC1:/home/test2# ls data file.txt root@PC1:/home/test2# [ -e file.txt ] root@PC1:/home/test2# echo $? ## 存在 0 root@PC1:/home/test2# [ -e xxx.txt ] root@PC1:/home/test2# echo $? ## 不存在 1 root@PC1:/home/test2# [ -e data ] root@PC1:/home/test2# echo $? ## 存在 0 root@PC1:/home/test2# [ -e dataxxx ] ## 不存在 root@PC1:/home/test2# echo $? 1
4、依据权限进行判断
root@PC1:/home/test2# ll total 12 drwxr-xr-x 3 root root 4096 5月 5 08:56 ./ drwxr-xr-x 10 root root 4096 5月 1 20:56 ../ drwxr-xr-x 2 root root 4096 5月 5 08:54 data/ -rw-r--r-- 1 root root 0 5月 5 08:53 file.txt root@PC1:/home/test2# [ -r file.txt ] ## 判断当前用户root是否对文件具有读的权限 root@PC1:/home/test2# echo $? ## 判断成功 0 root@PC1:/home/test2# [ -w file.txt ] root@PC1:/home/test2# echo $? ## 判读是否具有写的权限, 判断成功 0 root@PC1:/home/test2# [ -x file.txt ] ## 判断是否具有执行的权限, 判断失败 root@PC1:/home/test2# echo $? 1
5、逻辑而且、或者
&&:而且
||: 或者
root@PC1:/home/test2# [ -f file.txt ] && [ -d data ] root@PC1:/home/test2# echo $? ## &&两边同时为真才为真 0 root@PC1:/home/test2# [ -f file.txt ] && [ -f data ] root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ -f file.txt ] || [ -f data ] ## ||一边为真结构就为真 root@PC1:/home/test2# echo $? 0
等价:
root@PC1:/home/test2# [ -f file.txt -a -d data ] ## -a表示而且 root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ -f file.txt -a -f data ] root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ -f file.txt -o -f data ] ## -o表示或者 root@PC1:/home/test2# echo $? 0
6、判断文件是否为空
root@PC1:/home/test2# ll file.txt -rw-r--r-- 1 root root 0 5月 5 09:30 file.txt root@PC1:/home/test2# [ -s file.txt ] ## 文件非空时为真 root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# seq 10 > file.txt root@PC1:/home/test2# ll file.txt -rw-r--r-- 1 root root 21 5月 5 09:31 file.txt root@PC1:/home/test2# [ -s file.txt ] ## 文件非空, 判断为真 root@PC1:/home/test2# echo $? 0
7、数值判断
root@PC1:/home/test2# [ 5 -lt 3 ] ## lt:less than 表示小于 root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ 1 -lt 3 ] root@PC1:/home/test2# echo $? 0
root@PC1:/home/test2# [ 5 -gt 3 ] ## gt: greater than 表示大于 root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ 1 -gt 3 ] root@PC1:/home/test2# echo $? 1
root@PC1:/home/test2# [ 5 -eq 3 ] ## equal, 表示等于 root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ 3 -eq 3 ] root@PC1:/home/test2# echo $? 0
root@PC1:/home/test2# [ 3 -ne 5 ] ## ne表示 no equal root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ 3 -ne 3 ] root@PC1:/home/test2# echo $? 1
其他:-le: 小于等于; -ge:大于等于
8、字符串判断
root@PC1:/home/test2# [ a = a ] ## 相等判断 root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ a = b ] root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ a != a ] ## 不等判断 root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ a != b ] root@PC1:/home/test2# echo $? 0
判断变量是否为空:-z
root@PC1:/home/test2# a=abc root@PC1:/home/test2# b="" root@PC1:/home/test2# echo $a abc root@PC1:/home/test2# echo $b root@PC1:/home/test2# [ -z $a ] ## 非空为假 root@PC1:/home/test2# echo $? 1 root@PC1:/home/test2# [ -z $b ] ## 变量为空则为真 root@PC1:/home/test2# echo $? 0
9、结合命令语句
root@PC1:/home/test2# cat file.txt 1 2 3 4 5 root@PC1:/home/test2# [ $(cat file.txt | wc -l) -eq 5 ] ## 判断file.txt是否为5行 root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ $(cat file.txt | wc -l) -eq 10 ] ## 为假 root@PC1:/home/test2# echo $? 1
root@PC1:/home/test2# cat file.txt aaa bbb root@PC1:/home/test2# [ $(head -n 1 file.txt) = aaa ] ## 结合命令语句判断字符串 root@PC1:/home/test2# echo $? 0 root@PC1:/home/test2# [ $(sed -n '2p' file.txt) = aaa ] root@PC1:/home/test2# echo $? 1
这篇关于linux 中 常用的条件判断语句的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30[译]漫画SELinux概念
- 2024-03-29linux 移动文件
- 2024-03-28linux .so file
- 2024-03-28Linux 磁盘管理
- 2024-03-28Linux学习笔记(十三)磁盘管理(一):磁盘分区
- 2024-03-26linux 创建 文件
- 2024-03-25使用SecureCRT对Linux vim进行颜色设置
- 2024-03-202019-2020-12 20199317 《Linux内核原理与分析》 第十二周作业
- 2024-03-20Linux运维的第二周总结
- 2024-03-13how to count number of directories in linux