linux 中判断文件是否为空

 

001、

[root@PC1 test]# ls
[root@PC1 test]# touch a.txt               ## 测试文件
[root@PC1 test]# seq 6 > b.txt             ## 测试文件
[root@PC1 test]# cat a.txt
[root@PC1 test]# cat b.txt
1
2
3
4
5
6
[root@PC1 test]# [ -s a.txt ]             ## 如果文件不为空,则返回真,否则返回假
[root@PC1 test]# echo $?                  ## a.txt为空,返回假
1
[root@PC1 test]# [ -s b.txt ]             ## b.txt不为空, 返回真
[root@PC1 test]# echo $?
0
[root@PC1 test]# echo xxx > a.txt
[root@PC1 test]# [ -s a.txt ]
[root@PC1 test]# echo $?
0

 

posted @ 2022-12-08 22:35  小鲨鱼2018  阅读(1118)  评论(0编辑  收藏  举报