格式:test 测试条件

  1. 字符串测试:
  2. 注意空格:
  3. test str1 == str2 测试字符串是否相等
  4. test str1 != str2 测试字符串是否不相等
  5. test str1 测试字符串是否不为空
  6. test -n str1 测试字符串是否不为空
  7. test -z str1 测试字符串是否为空
  8. 整数测试
  9. test int1 -eq int2 测试整数是否相等
  10. test int1 -ge int2 测试int1是否>=int2
  11. test int1 -gt int2 测试int1是否>int2
  12. test int1 -le int2 测试int1是否<=int2
  13. test int1 -lt int2 测试int1是否<int2
  14. test int1 -ne int2 测试两个数是否不相等
  15. 文件测试
  16. test -d file 指定文件是否为目录
  17. test -f file 指定文件是否为常规文件
  18. test -x file 指定文件是否可执行
  19. test -r file 指定文件是否可读
  20. test -w file 指定文件是否可写
  21. test -a file 指定文件是否存在
  22. test -s file 指定文件大小是否非0


测试语句一般不单独使用,一般作为if语句的测试条件,如:

  1. if test "hello" == "hello" ;then
  2. commands....
  3. fi
  4. 上面语句也可简化为(注意[]与"之间的空格)
  5. if [ "hello" == "hello" ];then
  6. ....

看一段代码:

  1. #!/bin/bash
  2. if test "hello" == "hello" ;then
  3. echo "equals"
  4. else
  5. echo "not equals"
  6. fi
  7. if test -z "" ;then
  8. echo "str is null"
  9. fi
  10. if test -n "" ;then
  11. echo "str is not null"
  12. fi
  13. if test "9" ;then
  14. echo "not null"
  15. else
  16. echo "null"
  17. fi
  18. #easy way
  19. if [ "hello" == "hello" ];then
  20. echo "equals"
  21. else
  22. echo "not equals"
  23. fi
  24. if [ -f /root/test/test1 ];then
  25. echo "test1 is a file"
  26. elif [ -d /root/test/test1 ];then
  27. echo "test1 is a dir"
  28. else
  29. echo "i don't know the result"
  30. fi

执行效果:

shell入门之变量测试的更多相关文章

  1. shell入门之变量测试 分类: 学习笔记 linux ubuntu 2015-07-10 15:49 31人阅读 评论(0) 收藏

    格式:test 测试条件 字符串测试: 注意空格: test str1 == str2 测试字符串是否相等 test str1 != str2 测试字符串是否不相等 test str1 测试字符串是否 ...

  2. Shell - 简明Shell入门02 - 变量(Variable)

    示例脚本及注释 #!/bin/bash v1=test-variable_123 # 全局变量 v2=12345 v3='This is a test!' # 赋值语句使用单引号或双引号可以包含空格 ...

  3. Shell 编程 (变量和条件测试)

    变量: 1 . 变量声明 直接使用变量 + 赋值 #!/bin/bash NAME='HELLO WORD' echo $NAME 使用 declare 关键字声明 declare(选项)(参数) + ...

  4. [Shell]Bash变量:变量测试与内容替换

    --------------------------------------------------------------------------------------------- 变量测试是通 ...

  5. linux shell脚本之-变量极速入门与进阶(1)

    1,如果创建shell脚本? 使用任意文本编辑软件,一般为vim,创建.sh结尾的文件,在文件的最开头用 #!/bin/bash 注明shell的类型 如: ghostwu@dev:~/linux/s ...

  6. Shell - 简明Shell入门

    本文以示例和注释的方式,对Shell编程的基本知识点进行了总结和回顾,所有脚本均已做了基本的调试和验证. Shell - 简明Shell入门 01 - 第一个脚本 脚本的定义.执行方法以及echo命令 ...

  7. linux shell 入门

    本文是本人学习linux shell入门收集整理,不完全原创. 参考博文: http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html ...

  8. shell 入门学习

    目录 shell 入门学习 注释 执行与启动 变量 语法 调试 title: shell 入门学习 date: 2019/7/16 15:47:49 toc: true --- shell 入门学习 ...

  9. 10_Linux基础-SHELL入门1

    @ 目录 10_Linux基础-SHELL入门1 一. 输入输出重定向 二. 2个特殊文件 三. here document 四. tee命令 五. 清空文件内容 六. SHELL入门 SHELL的变 ...

随机推荐

  1. VK CUP2017 ROUND 1

    来自FallDream的博客.未经允许,请勿转载,谢谢. ---------------------------------------------------- 和ditoly组队打vkcup,原来 ...

  2. 关于HttpClient重试策略的研究

    一.背景 由于工作上的业务本人经常与第三方系统交互,所以经常会使用HttpClient与第三方进行通信.对于交易类的接口,订单状态是至关重要的. 这就牵扯到一系列问题: HttpClient是否有默认 ...

  3. VS2012代码对齐快捷键

    1.选中想要对齐的代码 2.全选代码后按住Ctrl+K,Ctrl+F键,就可以了

  4. Java 反射 Method threw 'java.lang.InstantiationException' exception.

    查看这个InstantiationException:异常的api所说的是: 当应用程序试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象无法被实例化时,抛出该 ...

  5. HttpClient 实现 get,post请求

    private String sendPost(Map<String,Object> data, String url) { CloseableHttpClient httpClient ...

  6. JVM程序计数器

    一.先来看看概念 多线程的Java应用程序:为了让每个线程正常工作就提出了程序计数器(Programe Counter Register),每个线程都有自己的程序计数器这样当线程执行切换的时候就可以在 ...

  7. js error

    0x800a0259 - JavaScript 运行时错误: 未知的运行时错误 <p id="navigatorInfo"></p> var txt = & ...

  8. Mysql索引介绍及常见索引的区别

    关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车.对于没有索引的表,单表查询可能几十万数据就是瓶颈,而通常大型 ...

  9. supervisor使用,配置和安装(包括监控守护进程httpd,keepalived)

    yum -y install supervisor(如果安装不成功,需要更新源,yum -y install epel) 或者: wget --no-check-certificate https:/ ...

  10. Java8 按照类属性去重

    测试po package com.shiwulian.test.po; public class Person {private String id;private String name;priva ...