shell中的test用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

1. 数值测试

-eq 等于为true
-ne 不等,为true
-gt 大于,为true
-ge 大于等于,为true
-lt 小于,为true
-le 小于等于,为true

```
#shell
#!/bin/sh
a=3
b=5
if test $a -eq $b;then
echo "$a=$b"
else
echo "$a!=$b" #3!=5
fi
if test $a -gt $b;then
echo "$a>$b"
else
echo "$a$b"
fi
```

2. 字符串测试

= 等于,为true
!= 不等,为true
-z 字符串 字符串的长度为0,为true
-n 字符串 字符串的长度不为0,为true

```
#shell
#!/bin/sh
a="hello"
b="world"
if test $a = $b;then
echo "equal"
else
echo "not equal" not equal
fi
if test -z $a;then
echo "字符串长度为0"
else
echo "字符串长度不为0" #字符串长度不为0
fi
```
## 3. 文件测试

-e 文件名 如果文件存在,则为真
-r 文件名 如果文件存在且可读,则为真
-w 文件名 如果文件存在且可写,则为真
-x 文件名 如果文件存在且可执行,则为真
-s 文件名 如果文件存在且至少有1个字符,则为真
-d 文件名 如果文件存在且是目录则为真
-f 文件名 如果文件存在且为普通文件,则为真
-c 文件名 如果文件存在且为字符型特殊文件,则为真
-b 文件名 如果文件存在且为特殊文件,则为真

```
#shell
#!/bin/sh
if test -e land;then
echo "文件已存在"
else
touch land
echo "文件已创建" #文件已创建
fi
if test -r land;then
echo "文件存在且可读" #文件存在且可读
else
echo "文件不存在或文件不可读"
fi
```

4. 使用逻辑运算符 -a -o !

#shell
#!/bin/sh
if test -e land -o -e wind;then
echo "有一个存在" #有一个存在
else
echo "2个都不存在"
fi

shell 9test命令的更多相关文章

  1. linux 基础 shell脚本命令

    #########shell脚本命令#### 1.diff diff file file1 ####比较两个文件的不同 -c ####显示周围的行 -u ####按照一格式统一输出生成补丁 -r ## ...

  2. paip.执行shell cmd 命令uapi java php python总结

    paip.执行shell cmd 命令uapi java php python总结 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:h ...

  3. (转)Hbase shell 常用命令(1)

    Hbase shell 常用命令(1) link:http://blog.csdn.net/scutshuxue/article/details/6988348 下面我们看看HBase Shell的一 ...

  4. Shell printf 命令

    Shell printf 命令 printf 命令模仿 C 程序库(library)里的 printf() 程序. 标准所定义,因此使用printf的脚本比使用echo移植性好. printf 使用引 ...

  5. Shell echo命令

    Shell echo命令 echo "It is a test" 这里的双引号完全可以省略 .显示变量 read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shel ...

  6. 进入BIOS SHELL DUMP 命令

    LINUX系统 进入SHELL 输入命令 fs1: or fs0: 就进入了U盘目录 然后输入 ACPIRW.efi  -d -s dsdt.bat 就会产生结果到U盘 ——————————————— ...

  7. Linux Shell : Test命令参数解析

    格式: test conditions test -n string : string 不为空 test -z string : string 为空 test int1 -eq int2  : int ...

  8. shell解析命令行的过程以及eval命令

    本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过程如下图: 以执行以下命令为例: echo -e ...

  9. Shell和命令基础

    什么是Shell Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解析器),Shell接收用户输入的命令并把它送入到内核去执行,结构如下图 Shell的功能 Shell最重要的 ...

随机推荐

  1. axure 认证授权注册码2017-12-25

    Licensee: University of Science and Technology of China (CLASSROOM)Key: DTXRAnPn1P65Rt0xB4eTQ+4bF5IU ...

  2. 玩转树莓派:OpenHAB的入门(一)

    如果你对那些仅仅只是为了控制一盏灯而不得不下载一个特定的App的智能家居感到厌烦,这里有个好消息:OpenHAB可以为你实现最灵活的控制智能家居,OpenHAB是一个成熟的,开源的家庭自动化平台,既可 ...

  3. 转:AMD规范与CMD规范的区别是什么?

    AMD规范与CMD规范的区别是什么?    在比较之前,我们得先来了解下什么是AMD规范?什么是CMD规范?当然先申明一下,我个人也是总结下而已,也是网上看到的资料,自己总结下或者可以说整理下而已,供 ...

  4. am335x Qt SocketCAN Demo hacking

    /*********************************************************************************** * am335x Qt Soc ...

  5. [LeetCode&Python] Problem 561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  6. ZOJ2401 Zipper 双塔式 DP(双塔DP)

    第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...

  7. 腾讯的模板引擎---artTemplate

    主要方法如下5种,在此不详细说artTemplate的方法,主要记录三种使用artTemplate的方法. template(id, data) 根据 id 渲染模板.内部会根据document.ge ...

  8. C# 使用GZip对字符串压缩和解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  9. 定时器setTimeout()的传参方法

    更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...

  10. string str将str转字符数组以及字符数组初始化

    c和指针(<pointers on c>) 8.1.11 一个字符串,如"hello",一般为字符串常量,可以用它对字符指针赋值,或初始化,相当于把这个字符串常量的首地 ...