if语句

if [ 条件判断式 ]
  then
    程序
elif [ 条件判断式 ]
  then
    程序
else
  程序
fi

注意:
  a.使用fi结尾
  b.条件判断式和中括号之间需要有空格

[root@localhost sh]# cat if_test.sh
#!/bin/bash
#判断系统硬盘使用率 rate=$(df -h | grep /dev/sda1 | awk '{print $5}' | cut -d "%" -f1) if [ $rate -ge 90 ]
  then
    echo "dev/sda1 is full"
    echo "now use : $rate"
elif [ $rate -ge 80 ]
  then
    echo "dev/sda1 will be full"
    echo "now is $rate"
else
  echo "dev/sda1 is not full"
  echo "now use : $rate"
fi [root@localhost sh]# sh if_test.sh
dev/sda1 is not full
now use : 7
[root@localhost sh]#

case语句

case $变量名 in
  "值1")
    如果值为1就执行这里的代码
    ;;
  "值2")
    如果值为2就执行这里的代码
    ;;
  *)
    如果都匹配不上就执行这里的代码
    ;;
esac

[root@localhost sh]# cat case_test.sh
#!/bin/bash
#判断用户输入 read -p "input yes/no: " -t 30 cho
case $cho in
  "yes")
    echo "intput is yes"
    ;;
  "no")
    echo "input is no"
    ;;
  *)
    echo "error input"
    ;;
esac [root@localhost sh]# sh case_test.sh
input yes/no: yes
intput is yes
[root@localhost sh]#

  

for语句

语法一:

for 变量 in 值1 值2 值3
  do
    程序
  done

[root@localhost sh]# cat for1.sh
#!/bin/bash
#打印时间
for time in moring noon afternoon evening
  do
    echo "This time is $time"
  done [root@localhost sh]# sh for1.sh
This time is moring
This time is noon
This time is afternoon
This time is evening
[root@localhost sh]#
[root@localhost sh]# cat for2.sh
#!/bin/bash
#打印文件名
ls > ls.log
for f in $(cat ls.log)
  do
    echo "File is $f"
  done [root@localhost sh]# sh for2.sh
File is case_test.sh
File is for1.sh
File is for2.sh
File is if_test.sh
File is ls.log
File is param_test1.sh
File is param_test2.sh
File is param_test3.sh
[root@localhost sh]#

语法二:

for ((初始值;循环控制条件;变量变化))
  do
    程序
  done

[root@localhost sh]# cat for3.sh
#!/bin/bash
#从1加到100
s=0
for((i=1;i<=100;i++))
  do
    s=$(($s+$i))
  done echo "Sum $s"
[root@localhost sh]# sh for3.sh
Sum 5050
[root@localhost sh]#

while循环
while [条件判断式]
  do
    程序
  done

[root@localhost sh]# cat while_test.sh
#!/bin/bash
#从1到100累加
i=1
s=0
while [ $i -le 100 ]
  do
    s=$(($s+$i))
    i=$(($i+1))
  done
echo "Sum $s"
[root@localhost sh]# sh while_test.sh
Sum 5050
[root@localhost sh]#

  

until循环
until [条件判断式]
  do
    程序
  done

[root@localhost sh]# cat until_test.sh
#!/bin/bash
#从1到100累加
i=1
s=0
until [ $i -gt 100 ]
  do
    s=$(($s+$i))
    i=$(($i+1))
  done
echo "Sum $s"
[root@localhost sh]# sh until_test.sh
Sum 5050
[root@localhost sh]#

  

Linux中的流程控制语句的更多相关文章

  1. linux shell awk 流程控制语句(if,for,while,do)详细介绍

    在linux awk的 while.do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出.break中断当前正在执行的循环并跳到循环 ...

  2. Mysq中的流程控制语句的用法

    这篇博客主要是总结一下Mysq中的流程控制语句的用法,主要是:CASE,IF,IFNULL,NULLIF 1.case CASE value WHEN [compare-value] THEN res ...

  3. SQL SERVER中的流程控制语句

    流程控制语句 是指用来控制程序运行和流程分至点额命令.一般指的是逻辑计算部分的控制. 1.Begin End语句 封装了多个T-SQL语句组合,将他们组成一个单元来处理. 一般在条件查询或者循环等控制 ...

  4. JS中的流程控制语句

    什么叫做语句? 语句:可以理解为语言中一句一句完整的话,程序是由一条条语句构成的,语句是按照自上往下的顺序执行的. 在JavaScript可以使用{  }来为语句进行分组.同一{  }中的语句称为一组 ...

  5. JavaScript基础&实战(3)js中的流程控制语句、条件分支语句、for循环、while循环

    文章目录 1.流程控制语句 1.1 代码 1.2 测试结果 2.弹窗提示输入内容 2.1 代码 2.2 测试结果 3.条件分支语句 3.1 代码 3.2 测试结果 4.while和 do...whil ...

  6. java中的流程控制语句总结

    程序的结构分类: 顺序结构:按照写代码的顺序 一次执行 选择结构:根据条件的不同有选择的执行不同的代码 循环结构:在一定条件下 反复执行某一片代码 选择结构: 也叫分支结构 根据条件的不同,有选择的执 ...

  7. Linux Shell 02 流程控制语句

    一.if语句格式:支持if/elif/else形式,支持嵌套 1. command执行成功(及退出状态为0)时,执行command2 2. 当判断条件为test命令时,判断结果为true时,执行com ...

  8. python 中的流程控制语句

    原文 if 语句 >>> x = int(input("Please enter an integer: ")) Please enter an integer: ...

  9. PHP:第二章——PHP中的流程控制语句

    if语句的集中形式 <?php /*if(条件) 语句; if(条件){语句块} if(条件){语句或语句块}else{语句或语句块} if(条件){语句或语句块}elseif(条件){语句或语 ...

随机推荐

  1. .NET下的ORM框架有哪些

    现在市面上针对.NET ORM框架越来越多 微软自家的LINQ to SQL, ADO.NET Entity Framework不多说. 商业: 1.LightSpeed(免费版比较垃圾.表限制8个. ...

  2. ELF解析(part one)

    the contents class elf { //date structure Elf32_Ehdr ehdr; Elf32_Shdr shdr; Elf32_Phdr phdr; // void ...

  3. UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

    UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...

  4. kafka 部分问题处理记录

    转载请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7600561.html 一  broker启动后ReplicaFetcherThread OOM 版 ...

  5. Python常见经典 python中if __name__ == '__main__': 的解析

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...

  6. Mysql 变量讲解

    set语句的学习: 使用select定义用户变量的实践将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties);我的修改: sele ...

  7. 创建动作-Action:

    在Struts2的行动,唯一的要求是,必须有一个无参数的方法,该方法返回一个字符串或结果的对象,必须是一个POJO.如果不带参数的方法不指定,则默认行为是使用execute()方法. 您也可以选择扩展 ...

  8. Openstack(Kilo)安装系列之环境准备(二)

    控制节点.网络节点.计算节点: 一.配置源 1.配置EPEL源 yum install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-rel ...

  9. python 自动化之路 day 19 Django基础[二]

    Django - 路由系统 url.py - 视图函数 views.py - 数据库操作 models.py - 模板引擎渲染 - HttpReponse(字符串) - render(request, ...

  10. 解决ios8 webView加载的地图无法定位问题

    本文转载至http://www.cocoachina.com/bbs/read.php?tid-237825.html     1.在文件info.pilist 中导入 NSLocationWhenI ...