Shell test命令
编程技术  /  houtizong 发布于 3年前   127
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
-eq | 等于则为真 |
-ne | 不等于则为真 |
-gt | 大于则为真 |
-ge | 大于等于则为真 |
-lt | 小于则为真 |
-le | 小于等于则为真 |
实例演示:
num1=100num2=100if test $[num1]-eq $[num2]then echo 'The two numbers are equal!'else echo 'The two numbers are not equal!'fi
输出结果:
The two numbers are equal!
= | 等于则为真 |
!= | 不相等则为真 |
-z 字符串 | 字符串长度伪则为真 |
-n 字符串 | 字符串长度不伪则为真 |
实例演示:
num1=100num2=100if test num1=num2then echo 'The two strings are equal!'else echo 'The two strings are not equal!'fi
输出结果:
The two strings are equal!
-e 文件名 | 如果文件存在则为真 |
-r 文件名 | 如果文件存在且可读则为真 |
-w 文件名 | 如果文件存在且可写则为真 |
-x 文件名 | 如果文件存在且可执行则为真 |
-s 文件名 | 如果文件存在且至少有一个字符则为真 |
-d 文件名 | 如果文件存在且为目录则为真 |
-f 文件名 | 如果文件存在且为普通文件则为真 |
-c 文件名 | 如果文件存在且为字符型特殊文件则为真 |
-b 文件名 | 如果文件存在且为块特殊文件则为真 |
实例演示:
cd /binif test -e ./bashthen echo 'The file already exists!'else echo 'The file does not exists!'fi
输出结果:
The file already exists!
另外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。例如:
cd /binif test -e ./notFile -o ./bashthen echo 'One file exists at least!'else echo 'Both dose not exists!'fi
输出结果:
One file exists at least!本文转自:http://www.w3cschool.cc/linux/linux-shell-test.html
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接