如何在shell中判断一个文件是否为空
2015-09-18 16:51
766 查看
在Linux中写脚本的时候,总免不了需要判断文件是否存在、文件内容是否为空等存在,而这些操作都可以用test 指令来实现,通过 man test 指令可以查看关于test指令的手册,手册中有如下说明:
-s FILE
FILE exists and has a size greater than zero
如果文件存在且文件大小大于零,则返回真
-e FILE
FILE exists
如果文件存在,则返回真
在shell中通过test指令测试文件是否为空的示例脚本如下:
在shell中,test指令还有另外一种写法,上面的脚本和下面的脚本是等价的:
--本篇文章主要转自:http://www.letuknowit.com/topics/20120402/linux-shell-test-command.html/
-s FILE
FILE exists and has a size greater than zero
如果文件存在且文件大小大于零,则返回真
-e FILE
FILE exists
如果文件存在,则返回真
在shell中通过test指令测试文件是否为空的示例脚本如下:
#! /bin/sh if test -s file.txt; then echo "hi" else echo "empty" fi
在shell中,test指令还有另外一种写法,上面的脚本和下面的脚本是等价的:
#! /bin/sh if [ -s file.txt ]; then echo "hi" else echo "empty" fi
--本篇文章主要转自:http://www.letuknowit.com/topics/20120402/linux-shell-test-command.html/
相关文章推荐
- File permissions: why can't I list a directory?
- SHELL编程基本知识点
- rsync错误捕获 shell捕获返回码
- shell测试
- 【ADB命令行】adb shell screenrecord命令行使用说明
- bash编程之:流程控制
- bash编程之:case语句、read与用户交互
- bash编程之:if语句
- bash编程之:算术运算
- shell中if做比较
- Powershell 查看软件是否成功安装
- Xshell配置ssh免密码登录
- adb shell下查看sqlite数据库
- 手动执行shell没问题,crontab执行shell却不行原因
- Calling Executable Files in PowerShell
- bash shell基础
- adb shell 命令详解,android
- shell学习笔记
- 【转】shell 教程——07 Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
- 【转】shell 教程——06 Shell变量:Shell变量的定义、删除变量、只读变量、变量类型