语法结构如下:

1. if-then语句

# if-then语句

if command   #根据conmmand的退出状态码,选择执行语句
then
commands
fi e.g.
#!usr/bin/bash
testuser=rich
if grep $testuser /etc/passwd
then
echo The bash files for user $testuser are:
ls -a /home/$testuser/
fi

2. if-then-else语句

 #!usr/bin/bash
if command #根据conmmand的退出状态码,选择执行语句
then
commands
else
commands
fi

3. 嵌套if(elif)

 #!usr/bin/bash
if command
then
commands
elif command
then
commands
elif command
then
commands
else
commands
fi

4. test命令(单测试条件)

 # 主要有3类测试条件
* 数值比较: -eq; -ge; -gt; -le; -lt; -ne.
* 字符串比较:=; !=; \<; \>; -n; -z.
* 文件比较:-d; -e; -f; -r; -s; -w; -x; -O; -G; -nt; -ot. #usr/bin/bash
if test condition
then
commands
fi #!usr/bin/bash
if [ condition ] # condition前后又空格
then
commands
fi

5. 复合条件测试

 # AND测试:必须同时满足两个条件(真)
if [ condition1] && [ condition2 ]
then
commands
fi #OR测试:满足任何一个条件(任一为真)
if [ conditon1 ] || [ condition2 ]
then
commands
fi

6. if-then高级特性

 # 双圆括号:用于数学表达式
(( expression ))
# 常用表达式:val++; val--; ++val; --val; !(逻辑求反); ~(位求反); **; <<(位左移); >>(位右移); &位布尔‘和’及‘或’; |; 逻辑‘和’及‘或’:&&; ||;
if (( expression ))
then
commands
(( expression ))
fi # 双方括号:用于字符串表达式(e.g. 正则表达式)
[[ expression ]] if [[ expression ]]
then
commands
fi

7. case命令

 # 将指定的变量同不同的模式进行比较

 case variable in
pattern1 | pattern2)
commands1;;
pattern3)
commands2;;
*)
commands3;;
esac

[Linux] 结构化命令 if的更多相关文章

  1. [linux] 结构化命令-for

    1 for命令 # for:迭代循环:默认空格为分隔符 for var in list do commands done 1.1 读取列表中的值 #!usr/bin/bash for test in ...

  2. Linux 结构化命令

    if -then 语句 if -then 语句有如下格式 if command then commands f i bash shell 的if语句会先运行if后面的那个命令,如果改命令的退出状态码是 ...

  3. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

  4. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  5. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

  6. bash&nbsp;shell笔记2&nbsp;结构化命令

    二.使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻辑控制流,有些命令允许脚本 ...

  7. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  8. bash&nbsp;shell笔记3&nbsp;结构化命令二

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/513601 三 ...

  9. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

随机推荐

  1. smarty 模板的入门使用

    <?php require_once 'inc/libs/Smarty.class.php'; $s=new Smarty(); // echo $s::SMARTY_VERSION; // e ...

  2. 自定义浏览器协议,实现web程序调用本地程序

    转自  http://blog.csdn.net/talking12391239/article/details/40712759 亲测可用 tencent://Message/?Uin=000000 ...

  3. Datasnap Image

    delphi用,不能与java.c#互相识别. procedure TServerMethods.UpdateDoc(ItemID : integer; doc : TStream); delphi用 ...

  4. Hbase资料汇总

    1.hbase api http://blog.csdn.net/u010967382/article/details/37878701

  5. unkow jdbc driver : http://maven.apache.org

    报了这么一个错,找了很久才找到问题出在哪里,具体为什么会什么出现现在还不怎么懂,只是现在能让它继续跑起来 这个错是因为我的spring-mybatis.xml文件读取不了jdbc.properties ...

  6. REST及RESTful的实现

    什么是REST? REST (REpresentation State Transfer) 描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding ...

  7. php限定时间内同一ip只能访问一次

    建立一个数据表 CREATE TABLE `clicks` ( `ip` INT UNSIGNED NOT NULL , `time1` INT UNSIGNED NOT NULL , `time2` ...

  8. c++多态

    #include <cstdio> using namespace std; class Base { public: virtual void A() { puts("Base ...

  9. 搭建DAO层和Service层代码

    第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...

  10. Java内部类,枚举

    模式: 模板模式: 简单工厂模式: 适配器模式:  interface ICellPhone  {   void sendMsg();  } class Android implements ICel ...