1.if—then
#!/bin/bash
if date              如果命令运行成功(退出码为0),则then部分的命令被执行
then
   echo "good"
fi

2.if—then—else
#!/bin/bash
if hunter
then
   echo "good"
else 
   echo "bad"        if语句中退出码非0,则执行else部分语句
fi

3.elif
#!/bin/bash
if hunter
then
   echo "command not found"
elif date            多重If判断
then
   echo "date"
fi

4.test命令           与其它编程语言思维一样的方式
#!/bin/bash
if test 1 -eq 2      test命令列出条件成立则退出并返回状态码0,命令等同if [ 1 -eq 2]
then
   echo "right"
else
   echo "wrong"
fi

5.条件比较
1)数据比较
n1 -eq n2            相等
n1 -ge n2            大于或等于
n1 -gt n2             大于
n1 -le n2             小于等于
n1 -lt n2              小于
n1 -ne n2            不等于
2)字符串比较
str1 = str2
str1 != str2
str1 < str2
str1 > str2
-n str1              检查str1的长度是否非0
-z str1              检查str1的长度是否为0

#!/bin/bash
str1=good
str2=wrong
if [ $str1 \> $str2 ]          大于号需要转义,否则脚本会当重定向符号
then echo "$str1 is greater than $str2"
else
   echo "$str1 is less than $str2"
fi

#!/bin/bash
str=
if [ -z "$str" ]               判断字符串是否为空,空的或未初始化的变量对Shell脚本很致命
then 
   echo "empty"
else
   echo "not empty"
fi

4)复合条件&&与、||或
#!/bin/bash
if [ -e str ] && [ -f zero ]
then
   echo "file all exist"
fi

5)双圆括号
#!/bin/bash
str1=good
str2=wrong
if (( $str1 > $str2 ))        双圆括号中表达式里的大于号不用转义
then echo "$str1 is greater than $str2"
else
   echo "$str1 is less than $str2"
fi

6)双方括号
#!/bin/bash

if [[ $USER == r* ]]          支持通配符模式
then
echo "welcome root user"
else
echo "sorry"
fi

  

7)case分支
#!/bin/bash
case $USER in
root|hunterno4)
   echo "welcome root user";;           尾部两个分号
mysql)
   echo "welcome to database";;
surfftp)
   echo "nice to meet you";;
*)                                      都不匹配时的default语句
   echo "i don't know you";;
esac

#! /bin/bash

str1=
str2=
if [ $str1 -gt $str2 ] #大于号需要转义,否则脚本会当重定向符号
then echo "$str1 is greater than $str2"
elif [ $str1 -eq $str2 ]
then echo "$str1 is equal to $str2"
else
echo "$str1 is less than $str2"
fi str="abc"
if [ -z $str ]
then
echo "empty"
else
echo "not empty, the string is: "$str
fi str3="aaa"
if [[ $str3="aaa" ]]
then echo "str3($str3) equals \"aaa\""
else
echo "not equals"
fi if test =
then echo "test "test" "
else echo "not equals"
fi

Linux Shell编程(1): 条件语句的更多相关文章

  1. 02 shell编程之条件语句

    Shell编程之条件语句 学习目标: 掌握shell脚本条件测试 掌握if语句编程 目录结构: 条件测试 条件测试概述 l  对特定的条件进行判断,以决定如何执行操作 l  测试的方法 方法1:tes ...

  2. if语句 条件测试 shell编程之条件语句

    shell 编程之条件语句一.条件测试  ① test命令 测试  ② 文件测试  ③ 整数值比较  ④ 字符串比较  ⑤ 逻辑测试二.if语句的结构  单分支结构  双分支结构  多分支结构三.ca ...

  3. Shell编程之条件语句:if、case语句

    Shell编程之条件语句:if.case语句               一.条件测试                1)test命令测试                2)整数值比较         ...

  4. shell编程之条件语句

    目录: 一.条件测试 1.test命令测试 2.文件测试 3.字符串比较 4.逻辑测试 二.if语句 1.if单分支语句 2.if双分支语句 3.if多分支语句 三.case语句 case多分支语句 ...

  5. Linux - 简明Shell编程05 - 条件语句(Case)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一 ...

  6. linux —— shell 编程(文本处理)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...

  7. linux —— shell 编程(编程语法)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...

  8. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  9. linux shell 编程参考

    #!/bin/bash my_fun() { echo "$#" } echo 'the number of parameter in "$@" is '$(m ...

随机推荐

  1. java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

    因为这个问题折腾了以上午,终于解决了,做下记录: 错误提示为:java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLSer ...

  2. 轻轻修改配置文件完成 OpenStack 监控

    当我们使用虚拟化云平台 OpenStack 时,必然要时时监控其虚拟机性能,随着近年企业级数据中心的不断发展,像混合虚拟化环境的业务需求也在持续增长中,因而也随之带来的监控需求更显重要,所以小编带来一 ...

  3. POJ 2499 Binary Tree(二叉树,找规律)

    题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...

  4. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  5. sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。

    SELECT TableName = obj.name, TotalRows = prt.rows, [SpaceUsed(KB)] = SUM(alloc.used_pages)* FROM sys ...

  6. 深入理解JVM—字节码执行引擎

    原文地址:http://yhjhappy234.blog.163.com/blog/static/3163283220122204355694/ 前面我们不止一次的提到,Java是一种跨平台的语言,为 ...

  7. form表单中的enctype属性什么意思?

    enctype就是encodetype翻译成中文就是编码类型的意思!multipart/form-data是指表单数据有多部分构成:既有文本数据,又有文件等二进制数据的意思.另外需要注意的是:默认情况 ...

  8. sublime 复制黏贴等快捷键修改

    在 keyboard-binding user 里 增加个人配置来覆盖默认配置 [ { "keys": ["ctrl+z"], "command&qu ...

  9. Project Euler 91:Right triangles with integer coordinates 格点直角三角形

    Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...

  10. 表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用

    1.表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用啊. 2.表连接产生的结果用于select/insert用 3.表连接产生的结果用于select/insert用 比如 ...