官方文档: help test

File operators:

      -a FILE        True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read. FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date). FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ef FILE2 True if file1 is a hard link to file2. String operators: -z STRING True if string is empty. -n STRING
STRING True if string is not empty. STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically. Other operators: -o OPTION True if the shell option OPTION is enabled.
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge. Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.

例子:

1. 判断文件是否存在

root@Director test]# test -f demo.sh
[root@Director test]# test -f demo.sh && echo true
true
[root@Director test]# test ! -f demo.sh && echo true
[root@Director test]#

2. 判断字符串是否为空

[root@Director test]# var=hello
[root@Director test]# test -n $var && echo true
true
[root@Director test]# test -z $var && echo true
[root@Director test]#

3.比较

[root@Director test]# var=a
[root@Director test]# test $var = a && echo true
true

Linux命令之乐--test的更多相关文章

  1. Linux命令之乐--grep

    正则表达式基本组成部分 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* St ...

  2. Linux命令之乐--cat

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 命令参数: -A, --show-all  ...

  3. Linux命令之乐--sed

    sed是stream edit的缩写,是处理文本非常重要的工具. 常见用法: 1. 替换文本 1.1 替换文本中的第一处符合的样式 sed 's/pattern/replace_string' fil ...

  4. Linux命令之乐--find

    find是命令行工具箱中最棒的命令之一. 列出当前目录及其子目录中的文件和文件夹. [root@LAMP WebRoot]# find . -print../index.jsp./upload.jsp ...

  5. Linux命令之乐--script和scriptplay

    script和scriptplay可以把终端会话记录到一个文件中,可以用来制作命令行教学视屏. 开始录制会话 [root@new test]# script -t >timing.log -a ...

  6. Linux命令之乐--md5sum

    md5sum命令用于生成和校验文件的md5值.它会逐位对文件的内容进行校验,它常用于检查文件的完整性. 读取文件的MD5值 [root@new ~]# md5sum /usr/local/sbin/* ...

  7. Linux命令之乐--read

    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 -p:后接屏幕输出的提示语句 -n:设定输入的字符个数,当达到指定的个数则自动退出,并将输入赋予给变量 -t:当输 ...

  8. Linux命令之乐--time

    time用于统计命令执行花费的总时间 例一: [root@Director usr]# time ls bin etc games include java lib lib64 libexec loc ...

  9. Linux命令之乐--awk

    1.脚本参数传值 #/bin/bash awk '"} {if(($1==a)) print $2;}' /etc/hosts 执行结果:

  10. Linux命令之乐--iconv

    用法: 实际应用: 批量转换文件编码: [root@wls12c PCK]$ for tfile in `ls -l|awk '{print $9}'`;do echo "iconv -f ...

随机推荐

  1. cat /proc/iomem

    在proc目录下有iomem和ioports文件,其主要描述了系统的io内存和io端口资源分布. 对于外设的访问,最终都是通过读写设备上的寄存器实现的,寄存器不外乎:控制寄存器.状态寄存器和数据寄存器 ...

  2. unity, Root Motion

    (引自:http://tieba.baidu.com/p/4323644080) 然后详细看了下这个文档:http://docs.unity3d.com/Manual/RootMotion.html ...

  3. Qt 插件综合编程-基于插件的OpenStreetMap瓦片查看器client(1)-墨卡托投影与坐标控制

    (相关的代码能够从https://github.com/goldenhawking/mercator.qtviewer.git直接克隆) 我们如今是准备做一个C/S架构的地图显示控件.就必定牵扯到坐标 ...

  4. android:Notification实现状态栏的通知

    在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和Notif ...

  5. struts2设置加载非默认路径的struts.xml文件解决方案

    方案一:   首先我们要明白struts2加载配置文件都是从它自己的jar包和\WEB-INF\classes两个默认的位置加载的,如果你想改变strusts2中的文件的默认加载路径,可以在web项目 ...

  6. tornado部署

    1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...

  7. swift 运算符和控制流程

    闭区间运算符 闭区间运算符(a...b)定义一个包含从a到b(包括a和b)的所有值的区间,只能是数字 for index in 1...5 { println("\(index) * 5 = ...

  8. python 快速获取文件类型

  9. 在echarts中自定义提示框内容

    1.期望效果 以柱状图为例,在鼠标滑过每个数据标签时,为了更友好地显示数据内容,需要对显示的数据内容作格式化处理,添加自定义内容. 如下图,鼠标滑过每个数据项时, 第1张是默认提示框: 第2张是处理成 ...

  10. [fork]Linux中的fork函数详解

    ---------------------------------------------------------------------------------------------------- ...