格式:test 测试条件

字符串测试:

注意空格:
test str1 == str2 测试字符串是否相等
test str1 != str2 测试字符串是否不相等
test str1 测试字符串是否不为空
test -n str1 测试字符串是否不为空
test -z str1 测试字符串是否为空

整数测试
test int1 -eq int2 测试整数是否相等
test int1 -ge int2 测试int1是否>=int2
test int1 -gt int2 测试int1是否>int2
test int1 -le int2 测试int1是否<=int2
test int1 -lt int2 测试int1是否<int2
test int1 -ne int2 测试两个数是否不相等

文件测试
test -d file 指定文件是否为目录
test -f file 指定文件是否为常规文件
test -x file 指定文件是否可执行
test -r file 指定文件是否可读
test -w file 指定文件是否可写
test -a file 指定文件是否存在
test -s file 指定文件大小是否非0


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

if test "hello" == "hello" ;then
commands....
fi

上面语句也可简化为(注意[]与"之间的空格)
if [ "hello" == "hello" ];then
....

看一段代码:

#!/bin/bash
if test "hello" == "hello" ;then
echo "equals"
else
echo "not equals"
fi
if test -z "" ;then
echo "str is null"
fi
if test -n "" ;then
echo "str is not null"
fi
if test "9" ;then
echo "not null"
else
echo "null"
fi
#easy way
if [ "hello" == "hello" ];then
echo "equals"
else
echo "not equals"
fi
if [ -f /root/test/test1 ];then
echo "test1 is a file"
elif [ -d /root/test/test1 ];then
echo "test1 is a dir"
else
echo "i don't know the result"
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. 笔记14 Hello Spring MVC

    搭建一个SpringMVC所需要的包: 1.创建MySpringMVC项目,使用dynamic web project的方式. 2.导入jar包,放到/WebContent(WebRoot)/WEB- ...

  2. 《Java技术》第一次作业——Java语言基础

    学习总结 Scanner类实现基本数据输入的方法 Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配.然后可以使用不同的 next 方法将得到的标记转换为不同类型的值. ...

  3. Linux系统中安装Oracle过程记录

    第一章 安装数据库软件 1.1 修改密码及创建目录和权限 创建oracle用户和组 创建相关目录并赋权 1.2 设置oracle用户环境变量 ORACLE_BASE:产品基目录 ORACLE_HOME ...

  4. AJAX 向后台发送带 List 集合的对象

    现有基类: public class School { int name; int address; List<Student> students = new ArrayList<S ...

  5. Vue nextTick 机制

    背景 我们先来看一段Vue的执行代码: export default { data () { return { msg: 0 } }, mounted () { this.msg = 1 this.m ...

  6. Oracle中表字段相关操作举例

    --创建测试表 create or replace table student ( xh ), --学号 xm ), --姓名 sex ), --性别 birthday date, --日期 sal ...

  7. 推荐几种PHP实现页面跳转的方法

    1.PHP实现页面跳转第一种方法 <?php header("Location:http://www.baidu.com"); ?> header()是php内置函数, ...

  8. ng-book札记——依赖注入

    依赖注入是一种使程序的一部分能够访问另一部分的系统,并且可以通过配置控制其行为. "注入"可以理解为是"new"操作符的一种替代,不再需要使用编程语言所提供的& ...

  9. Go 语言环境安装

    Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Window 安装包下载地址为:https://golang.org/dl/. 各个系统对应的包名: 操 ...

  10. [LaTeX] box algorithm adjustbox subfigures, textwidth

    algorithm \begin{figure}[!t] \vspace*{\dimexpr\fboxsep+\fboxrule} \fbox{% \parbox{\dimexpr\linewidth ...