Shell test命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
数值测试
| 参数 | 说明 |
|---|---|
| -eq | 等于则为真 |
| -ne | 不等于则为真 |
| -gt | 大于则为真 |
| -ge | 大于等于则为真 |
| -lt | 小于则为真 |
| -le | 小于等于则为真 |
实例演示:
num1=100
num2=100
if 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=100
num2=100
if test num1=num2
then
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 /bin
if test -e ./bash
then
echo 'The file already exists!'
else
echo 'The file does not exists!'
fi
输出结果:
The file already exists!
另外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。例如:
cd /bin
if test -e ./notFile -o ./bash
then
echo 'One file exists at least!'
else
echo 'Both dose not exists!'
fi
输出结果:
One file exists at least!
Shell test命令的更多相关文章
- linux 基础 shell脚本命令
#########shell脚本命令#### 1.diff diff file file1 ####比较两个文件的不同 -c ####显示周围的行 -u ####按照一格式统一输出生成补丁 -r ## ...
- paip.执行shell cmd 命令uapi java php python总结
paip.执行shell cmd 命令uapi java php python总结 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:h ...
- (转)Hbase shell 常用命令(1)
Hbase shell 常用命令(1) link:http://blog.csdn.net/scutshuxue/article/details/6988348 下面我们看看HBase Shell的一 ...
- Shell printf 命令
Shell printf 命令 printf 命令模仿 C 程序库(library)里的 printf() 程序. 标准所定义,因此使用printf的脚本比使用echo移植性好. printf 使用引 ...
- Shell echo命令
Shell echo命令 echo "It is a test" 这里的双引号完全可以省略 .显示变量 read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shel ...
- 进入BIOS SHELL DUMP 命令
LINUX系统 进入SHELL 输入命令 fs1: or fs0: 就进入了U盘目录 然后输入 ACPIRW.efi -d -s dsdt.bat 就会产生结果到U盘 ——————————————— ...
- Linux Shell : Test命令参数解析
格式: test conditions test -n string : string 不为空 test -z string : string 为空 test int1 -eq int2 : int ...
- shell解析命令行的过程以及eval命令
本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过程如下图: 以执行以下命令为例: echo -e ...
- Shell和命令基础
什么是Shell Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解析器),Shell接收用户输入的命令并把它送入到内核去执行,结构如下图 Shell的功能 Shell最重要的 ...
- Linux从入门到进阶全集——【第十四集:Shell基础命令】
1,Shell就是命令行执行器 2,作用:将外层引用程序的例如ls ll等命令进行解释成01表示的二进制代码给内核,从而让硬件执行:硬件的执行结果返回给shell,shell解释成我们能看得懂的代码返 ...
随机推荐
- BZOJ1611: [Usaco2008 Feb]Meteor Shower流星雨
1611: [Usaco2008 Feb]Meteor Shower流星雨 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 904 Solved: 393 ...
- HDU 5501 背包问题
需要按照B/C的值从大到小排序. #include<cstdio> #include<cstring> #include<iostream> #include< ...
- ink_test
- Safecracker(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1015 / 题意; 从所给的一串字符串中选出5个字母假如是(A B C D E)使得A-B^2+C^3-D^4+E ...
- 《ACM国际大学生程序设计竞赛题解I》——6.8
Poj1068: Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in ...
- Cstyle的UEFI导读之SEC第一篇 Reset Vector
最近小看了一下SEC部分的code,现在来做个总结.所谓SEC就是CPU刚刚完成硬件初始化的是时候执行的和CPU体系架构息息相关的代码.主要是为后续CPU以及Chipset初始化代码所需的必备 ...
- .NET 基础 一步步 一幕幕[面向对象之new、this关键字]
经常会有人问:小伙子,有没有对象啊,要不要大叔我帮你介绍一个啊,小伙子会说:大叔,不用我自己new一个就好了.(PS:活该你没有对象) 上边当然是一个段子咯,程序员那会没有对象,自己new一个就有了啊 ...
- smail 语法参考
Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. Depending on th ...
- 小学生玩ACM----深搜
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 解决"the currently displayed page contains invalid values"
原因是你的工程的根目录少了default.properties(有点项目工程这个文件名称是project.properties)这个文件,导致不能选择target: 解决办法: 在工程根目录下建立 ...