[Shell]test命令使用指南
来源:互联网 发布:阿里云 cdn 带宽限制 编辑:程序博客网 时间:2024/06/08 10:13
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
1. 数值
Example:
num1=100num2=100if test $[num1] -eq $[num2]then echo '两个数相等'else echo '两个数不相等'fi# 两个数相等
2. 字符串
Example:
str1="2016-11-21"str2="2016-11-22"if test ${str1} = ${str2}then echo '两个日期相同'else echo '两个日期不相同'fi# 两个日期不相同
备注
如果上面的使用方法中,字符串中有空格,则会报错:
str1="2016-11-22 12:34:21"str2="2016-11-22 12:34:21"if test ${str1} = ${str2}then echo '两个日期相同'else echo '两个日期不相同'fi
输出:
两个日期不相同/home/xiaosi/code/openDiary/BaseOperation/src/main/sh/sh_test.sh: 第 6 行: test: 参数太多
解决方案:
str1="2016-11-22 12:34:21"str2="2016-11-22 12:34:21"if test "${str1}" = "${str2}"then echo '两个日期相同'else echo '两个日期不相同'fi
输出:
两个日期相同
3. 文件
Example:
if test -e /home/xiaosi/error.txtthen echo '文件存在'else echo '文件不存在'fi# 文件存在if test -r /home/xiaosi/error.txtthen echo '文件可读'else echo '文件不可读'fi# 文件可读if test -s /home/xiaosi/error.txtthen echo '文件不为空'else echo '文件为空'fi# 文件为空if test -d /home/xiaosithen echo '文件为目录'else echo '文件不为目录'fi# 文件为目录
4. 逻辑操作
Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。
Example:
str="2016-11-21"if test "${str}" = "2016-11-21" -a -s /home/xiaosi/error.txtthen echo '日期正确 并且 文件不为空'else echo '日期错误 或者 文件为空'fi# 日期错误 或者 文件为空
阅读全文
1 0
- [Shell]test命令使用指南
- [Shell]tr命令使用指南
- shell中的test命令
- shell 笔记 test命令
- Linux Shell Test 命令
- shell 的test命令
- SHELL test [ 命令用法
- shell中的test命令
- shell 之 test 命令
- SHELL test [ 命令用法
- Linux shell test,[...] 命令
- shell命令test
- shell test命令用法
- SHELL test 命令用法
- Shell test命令
- shell命令-test
- SHELL test [ 命令用法
- Shell test命令
- 头文件重复包含和变量重复定义
- ExtJs 学习笔记(二)添加/删除子控件
- jvm系列(一):java类的加载机制
- android真机通过monkey进行稳定性测试与压力测试
- 排序算法.md
- [Shell]test命令使用指南
- Running OpenBTS with the Nuand bladeRF on Ubuntu (The Definitive and Step by Step Guide)
- C#WebService 出现Access-Control-Allow-Origin header is present on the requested resource无响应解决方法
- 快速排序.md
- js 中的对象原型
- 单位班车手持刷卡终端,同时可作考勤、消费刷卡终端
- JSP页面参数传递时类型转换总结
- 数控机床安装调试中的传动比设置
- jvm系列(二):JVM内存结构