if语句

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

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

  1. [root@localhost sh]# cat if_test.sh
  2. #!/bin/bash
  3. #判断系统硬盘使用率
  4.  
  5. rate=$(df -h | grep /dev/sda1 | awk '{print $5}' | cut -d "%" -f1)
  6.  
  7. if [ $rate -ge 90 ]
  8.   then
  9.     echo "dev/sda1 is full"
  10.     echo "now use : $rate"
  11. elif [ $rate -ge 80 ]
  12.   then
  13.     echo "dev/sda1 will be full"
  14.     echo "now is $rate"
  15. else
  16.   echo "dev/sda1 is not full"
  17.   echo "now use : $rate"
  18. fi
  19.  
  20. [root@localhost sh]# sh if_test.sh
  21. dev/sda1 is not full
  22. now use : 7
  23. [root@localhost sh]#

case语句

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

  1. [root@localhost sh]# cat case_test.sh
  2. #!/bin/bash
  3. #判断用户输入
  4.  
  5. read -p "input yes/no: " -t 30 cho
  6. case $cho in
  7.   "yes")
  8.     echo "intput is yes"
  9.     ;;
  10.   "no")
  11.     echo "input is no"
  12.     ;;
  13.   *)
  14.     echo "error input"
  15.     ;;
  16. esac
  17.  
  18. [root@localhost sh]# sh case_test.sh
  19. input yes/no: yes
  20. intput is yes
  21. [root@localhost sh]#

  

for语句

语法一:

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

  1. [root@localhost sh]# cat for1.sh
  2. #!/bin/bash
  3. #打印时间
  4. for time in moring noon afternoon evening
  5.   do
  6.     echo "This time is $time"
  7.   done
  8.  
  9. [root@localhost sh]# sh for1.sh
  10. This time is moring
  11. This time is noon
  12. This time is afternoon
  13. This time is evening
  14. [root@localhost sh]#
  1. [root@localhost sh]# cat for2.sh
  2. #!/bin/bash
  3. #打印文件名
  4. ls > ls.log
  5. for f in $(cat ls.log)
  6.   do
  7.     echo "File is $f"
  8.   done
  9.  
  10. [root@localhost sh]# sh for2.sh
  11. File is case_test.sh
  12. File is for1.sh
  13. File is for2.sh
  14. File is if_test.sh
  15. File is ls.log
  16. File is param_test1.sh
  17. File is param_test2.sh
  18. File is param_test3.sh
  19. [root@localhost sh]#

语法二:

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

  1. [root@localhost sh]# cat for3.sh
  2. #!/bin/bash
  3. #从1加到100
  4. s=0
  5. for((i=1;i<=100;i++))
  6.   do
  7.     s=$(($s+$i))
  8.   done
  9.  
  10. echo "Sum $s"
  11. [root@localhost sh]# sh for3.sh
  12. Sum 5050
  13. [root@localhost sh]#

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

  1. [root@localhost sh]# cat while_test.sh
  2. #!/bin/bash
  3. #从1到100累加
  4. i=1
  5. s=0
  6. while [ $i -le 100 ]
  7.   do
  8.     s=$(($s+$i))
  9.     i=$(($i+1))
  10.   done
  11. echo "Sum $s"
  12. [root@localhost sh]# sh while_test.sh
  13. Sum 5050
  14. [root@localhost sh]#

  

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

  1. [root@localhost sh]# cat until_test.sh
  2. #!/bin/bash
  3. #从1到100累加
  4. i=1
  5. s=0
  6. until [ $i -gt 100 ]
  7.   do
  8.     s=$(($s+$i))
  9.     i=$(($i+1))
  10.   done
  11. echo "Sum $s"
  12. [root@localhost sh]# sh until_test.sh
  13. Sum 5050
  14. [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. 使用swap 清空vector

    //最简单的使用swap,清除元素并回收内存 vector <int>().swap(vecInt); //清除容器并最小化它的容量, // vecInt.swap(vector<i ...

  2. 史上最简单的springcloud微服务入门实例,满足企业日常需求,开箱即用,工资翻倍不是梦

    在传统的IT行业软件大多都是各种独立系统的堆砌,这些系统的问题总结来说就是扩展性差,可靠性不高,维护成本高.到后面引入了SOA服务化,但是,由于 SOA 早期均使用了总线模式,这种总线模式是与某种技术 ...

  3. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  4. _T("") vs L 到底用谁?L!

    一直没有注意这个,今天突然纠结起来这个问题,代码写多了,难免这两个混用. 现在是时候有个结论了: 如果你的工程是unicode编译,那么请明确的使用L! 如果是多字节(ansi),那么请使用_T(&q ...

  5. 如何获取wifi名称(SSID)

    @import SystemConfiguration.CaptiveNetwork; /** Returns first non-empty SSID network info dictionary ...

  6. OpenCV中Kinect的使用(1)

    图像处理中一般为了更好的获取外部信息都会使用到Kinect,其优势在于除了传统的RGB摄像头之外,还拥有一个获取深度信息的3D深度感应器,因此可以获得外界物体的3维信息实现物体的跟踪.手势识别等各项功 ...

  7. -webkit-transition: all .2s ease-in-out;

    W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...

  8. (转)Unity3d游戏开场CG动画播放方式

    1.在一个plane上播放 1 2 3 4 5 6 7 8 9 10 11 12 using UnityEngine; using System.Collections; public class M ...

  9. [Python2.x] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

  10. 多个 python的pip版本选择

    如果你电脑里面装了多个版本的python python3 -m pip instatll xlutilspython2 -m pip instatll xlutils 加载新的pippython -m ...