Shell——test 命令
来源:互联网 发布:万网域名注册流程图 编辑:程序博客网 时间:2024/05/21 08:01
Shell test 命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
数值测试
实例:
num1=100num2=100if test $[num1] -eq $[num2]then echo '两个数相等!'else echo '两个数不相等!'fi
输出结果:
两个数相等!
(但上例中的[]是可以省略的)
代码中的 [] 执行基本的算数运算,如:
#!/bin/basha=5b=6result=$[a+b] # 注意等号两边不能有空格echo "result 为: $result"
结果为:
result 为: 11
字符串测试
实例:
num1="konsy"num2="konsy2"if test $num1 = $num2then echo 'The two strings are the same!'else echo 'The two strings are not the same!'fi
输出结果:
The two strings are not the same!
文件测试
实例:
cd /binif test -e ./bashthen echo '文件已存在!'else echo '文件不存在!'fi
输出结果:
文件已存在!
另外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:”!”最高,”-a”次之,”-o”最低。例如:
cd /binif test -e ./notFile -o -e ./bashthen echo '有一个文件存在!'else echo '两个文件都不存在'fi
输出结果:
有一个文件存在!
0 0
- Shell——test 命令
- Shell系列—Shell test 命令
- shell脚本——test命令的操作使用
- 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 [ 命令用法
- vsftp的配置安装
- Effective c# 第二版读书笔记1
- Tomcat9 users and roles, Remote deploy
- Delphi 10 Seattle小票打印控件TQ_Printer
- Shell——文件包含
- Shell——test 命令
- 接口与继承
- vim小技巧中恢复/撤销/复制部分字符
- Shell——流程控制
- Shell——函数
- [51nod 1136 欧拉函数]
- MySQL中REGEXP正则表达式
- MATALB中的最大和最小常量
- Android 软键盘弹出时把布局顶上去,控件乱套解决方法