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(条件){语句或语 ...
随机推荐
- RabbitMQ Performance Testing Tool 性能测试工具
RabbitMQ Performance Testing Tool 介绍:https://www.rabbitmq.com/java-tools.html RabbitMQ Performance T ...
- c#中从string数组转换到int数组及比较两个字符串相等
string[] input = { "1", "2", "3", "4", "5", " ...
- TensorFlow学习笔记 补充1——InteractiveSession
InteractiveSession 大家有时候在阅读代码时会看见InteractiveSession而不是熟悉的Session,这是什么东东呢? 其实,它们只有一点不同..... Interacti ...
- jquery ajax 脑图
- android动画具体解释二 属性动画原理
property动画是一个强大的框架,它差点儿能使你动画不论什么东西. 你能够定义一个动画来改变对象的不论什么属性,不论其是否被绘制于屏幕之上. 一个属性动画在一定时间内多次改变一个属性(对象的一个字 ...
- Drawable资源的初步使用
刚開始接触到Android的时候,看到类似以下的一个Button: 当时感觉这种button有点像Material Design风格.真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁 ...
- C++语言基础(12)-虚函数
一.虚函数使用的注意事项 1.只需要在虚函数的声明处加上 virtual 关键字,函数定义处可以加也可以不加. 2.为了方便,你可以只将基类中的函数声明为虚函数,这样所有子类中具有遮蔽(覆盖)关系的同 ...
- mysql5.7.22 zip 版安装
2.将zip文件解压到本地,本文解压到如下目录:D:\softwares\mysql-5.7.14-winx64 3.新建一个配置文件(my.ini)用于配置字符集.端口等信息,用以覆盖原始的配置文件 ...
- mysql的binlog和slow_log慢日志
redo undo 锁 ----------------------------------------- 日志管理 log-error=/var/log/mysql.log 二进制日志的“总闸” 作 ...
- chrono
时间段的表示 tmplate<class Rep,class Period=ratio<1>> class duration; duration类被用来表示时间段的计量器,Re ...