Linux中的流程控制语句
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中的流程控制语句的更多相关文章
- linux shell awk 流程控制语句(if,for,while,do)详细介绍
在linux awk的 while.do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出.break中断当前正在执行的循环并跳到循环 ...
- Mysq中的流程控制语句的用法
这篇博客主要是总结一下Mysq中的流程控制语句的用法,主要是:CASE,IF,IFNULL,NULLIF 1.case CASE value WHEN [compare-value] THEN res ...
- SQL SERVER中的流程控制语句
流程控制语句 是指用来控制程序运行和流程分至点额命令.一般指的是逻辑计算部分的控制. 1.Begin End语句 封装了多个T-SQL语句组合,将他们组成一个单元来处理. 一般在条件查询或者循环等控制 ...
- JS中的流程控制语句
什么叫做语句? 语句:可以理解为语言中一句一句完整的话,程序是由一条条语句构成的,语句是按照自上往下的顺序执行的. 在JavaScript可以使用{ }来为语句进行分组.同一{ }中的语句称为一组 ...
- JavaScript基础&实战(3)js中的流程控制语句、条件分支语句、for循环、while循环
文章目录 1.流程控制语句 1.1 代码 1.2 测试结果 2.弹窗提示输入内容 2.1 代码 2.2 测试结果 3.条件分支语句 3.1 代码 3.2 测试结果 4.while和 do...whil ...
- java中的流程控制语句总结
程序的结构分类: 顺序结构:按照写代码的顺序 一次执行 选择结构:根据条件的不同有选择的执行不同的代码 循环结构:在一定条件下 反复执行某一片代码 选择结构: 也叫分支结构 根据条件的不同,有选择的执 ...
- Linux Shell 02 流程控制语句
一.if语句格式:支持if/elif/else形式,支持嵌套 1. command执行成功(及退出状态为0)时,执行command2 2. 当判断条件为test命令时,判断结果为true时,执行com ...
- python 中的流程控制语句
原文 if 语句 >>> x = int(input("Please enter an integer: ")) Please enter an integer: ...
- PHP:第二章——PHP中的流程控制语句
if语句的集中形式 <?php /*if(条件) 语句; if(条件){语句块} if(条件){语句或语句块}else{语句或语句块} if(条件){语句或语句块}elseif(条件){语句或语 ...
随机推荐
- 使用swap 清空vector
//最简单的使用swap,清除元素并回收内存 vector <int>().swap(vecInt); //清除容器并最小化它的容量, // vecInt.swap(vector<i ...
- 史上最简单的springcloud微服务入门实例,满足企业日常需求,开箱即用,工资翻倍不是梦
在传统的IT行业软件大多都是各种独立系统的堆砌,这些系统的问题总结来说就是扩展性差,可靠性不高,维护成本高.到后面引入了SOA服务化,但是,由于 SOA 早期均使用了总线模式,这种总线模式是与某种技术 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- _T("") vs L 到底用谁?L!
一直没有注意这个,今天突然纠结起来这个问题,代码写多了,难免这两个混用. 现在是时候有个结论了: 如果你的工程是unicode编译,那么请明确的使用L! 如果是多字节(ansi),那么请使用_T(&q ...
- 如何获取wifi名称(SSID)
@import SystemConfiguration.CaptiveNetwork; /** Returns first non-empty SSID network info dictionary ...
- OpenCV中Kinect的使用(1)
图像处理中一般为了更好的获取外部信息都会使用到Kinect,其优势在于除了传统的RGB摄像头之外,还拥有一个获取深度信息的3D深度感应器,因此可以获得外界物体的3维信息实现物体的跟踪.手势识别等各项功 ...
- -webkit-transition: all .2s ease-in-out;
W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...
- (转)Unity3d游戏开场CG动画播放方式
1.在一个plane上播放 1 2 3 4 5 6 7 8 9 10 11 12 using UnityEngine; using System.Collections; public class M ...
- [Python2.x] 利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...
- 多个 python的pip版本选择
如果你电脑里面装了多个版本的python python3 -m pip instatll xlutilspython2 -m pip instatll xlutils 加载新的pippython -m ...