一、if

首先看if是一个shell关键字:

[root@node1 shell]# type if
if is a shell keyword
[root@node1 shell]# help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fiExecute commands based on conditional.The `if COMMANDS' list is executed.  If its exit status is zero, then the`then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list isexecuted in turn, and if its exit status is zero, the corresponding`then COMMANDS' list is executed and the if command completes.  Otherwise,the `else COMMANDS' list is executed, if present.  The exit status of theentire construct is the exit status of the last command executed, or zeroif no condition tested true.Exit Status:Returns the status of the last command executed.

通过help命令,我们已经清楚了它的语法:if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]… [ else COMMANDS; ] fi,下面就以例子来说明一下:

[root@node1 shell]# if test 3 -gt 2;then echo "ok";fi
ok
[root@node1 shell]# if test 3 -gt 5;then echo "ok";else echo "error"; fi
error
[root@node1 shell]# if test 3 -eq 4;then echo ok;elif test 3 -eq 3;then echo 3;fi
3

二、while

[root@node1 shell]# type while
while is a shell keyword
[root@node1 shell]# help while
while: while COMMANDS; do COMMANDS; doneExecute commands as long as a test succeeds.Expand and execute COMMANDS as long as the final command in the`while' COMMANDS has an exit status of zero.Exit Status:Returns the status of the last command executed.

通过help命令,我们可以了解到while的语法:while COMMANDS; do COMMANDS; done

[root@node1 shell]# while ls /share;do echo ok;rm -rf /share;done
ls: 无法访问/share: 没有那个文件或目录
[root@node1 shell]#

输出1~5:

[root@node1 shell]# while test $a -le 5;do echo $a;((a++));done
1
2
3
4
5

三、for

[root@node1 shell]# type for
for is a shell keyword
[root@node1 shell]# help for
for: for NAME [in WORDS ... ] ; do COMMANDS; doneExecute commands for each member in a list.The `for' loop executes a sequence of commands for each member in alist of items.  If `in WORDS ...;' is not present, then `in "$@"' isassumed.  For each element in WORDS, NAME is set to that element, andthe COMMANDS are executed.Exit Status:Returns the status of the last command executed.
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; doneArithmetic for loop.Equivalent to(( EXP1 ))while (( EXP2 )); doCOMMANDS(( EXP3 ))doneEXP1, EXP2, and EXP3 are arithmetic expressions.  If any expression isomitted, it behaves as if it evaluates to 1.Exit Status:Returns the status of the last command executed.

通过help,我们了解到for的语法为:1.增强for循环:for NAME [in WORDS … ] ; do COMMANDS; done 2.普通for循环:for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done

输出1~5:

[root@node1 shell]# for((a=1;a<=5;a++));do echo $a;done
1
2
3
4
5
[root@node1 shell]# for i in 1 2 3 4 5;do echo $i;((i++));done
1
2
3
4
5
[root@node1 shell]# for i in `seq 5`;do echo $i;((i++));done
1
2
3
4
5

练习题:

  • 用户给定路径
  • 输出文件大小最大的文件
  • 递归子目录

思路指引:du -a /tmp/ |sort -nr(忘记sort命令的可以点此查看!)

#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -a $1 | sort -nr`;doecho $i
done
IFS=$oldIFS

接下来我们要判断file并输出:

#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -a $1 | sort -nr`;dofilename=`echo $i | awk '{print $2}'`if [ -f $filename ];thenecho $filenamebreakfi
done
IFS=$oldIFS

最后来一个案例练习!

Shell-流程控制相关推荐

  1. shell 脚本比较字符串相等_LINUX快速入门第十六章:Shell 流程控制

    Shell 流程控制 和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?phpif (isset($_GET["q"])) { s ...

  2. Shell——流程控制

    Shell 流程控制 和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) {s ...

  3. Linux | Shell 学习笔记(二)Shell 流程控制 if、case、for、while| read读取输入 | 函数的使用 | cut、sed、awk、sort命令 +Demo

    文章目录 参考资料 运行环境 一.流程控制 1.1 if 判断 1.2 case 语句 1.3 for 循环 1.4 while 循环 二. read 读取控制台输入 三.函数 3.1 系统函数 ba ...

  4. linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例 --转载

    http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html nux shell有一套自己的流程控制语句,其中包括条件语句(if),循环 ...

  5. linux shell 流程控制

    导读 和Java.PHP等语言不一样,linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case).下面我将通过例子介绍下,各个语句使 ...

  6. linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例

    linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case).下面我将通过例子介绍下,各个语句使用方法. 一.shell条件语句(if ...

  7. 3.Linux Shell流程控制

    1.if/else结构 if condition thenstatements elif condition thenstatements elsestatements fi 2.条件 与C语言不同的 ...

  8. false shell 判断_六、Shell流程控制-if判断语句

    1. shell中的运算 1.1 数学比较运算 -eq 等于 -ne 不等于 -lt 小于 -gt 大于 -le 小于等于 -ge 大于等于 浮点数的比较建议将数字等比放大至整数进行比较,例如 #比较 ...

  9. Shell——流程控制(if、case、for、while)

    文章目录 一.if语句 1.基本语法 2.注意事项 3.示例演示 三.case语句 1.基本语法 2.注意事项 3.示例演示 二.for循环 1.基本语法 2.示例演示 四.while循环 1.基本语 ...

  10. Shell流程控制之until

    基本语法 until condition dodosomething... done until与while正好相反,until会一直执行循环体中的命令,直到condition为true位置.cond ...

最新文章

  1. matlab中(1 )什么意思,matlab中area(1)什么意?mat – 手机爱问
  2. 【数理知识】《积分变换与场论》王振老师-第4章-矢量分析
  3. OpenCL-3-同步机制
  4. C/C++基础知识:函数指针和指针函数的基本概念
  5. HTML5新特征、窍门和技术(6~10)
  6. linux桌面隐藏鼠标,如何隐藏鼠标光标
  7. python优先级排序_用Python实现优先级队列的3种方法
  8. Java项目迁移到uap上_Tomcat启动,不能加载项目问题。
  9. ubuntu服务器无法运行chromedriver解决方法(转)
  10. systemback Linux 系统备份、迁移
  11. python 爬取直播_python 斗鱼直播间爬取代码
  12. PROTUES实例——stm32点灯
  13. bootstarp怎么使盒子到最右边_疯狂搞机 | 联通IPTV盒子免拆安装第三方软件
  14. 一元函数,多元函数,可微的含义 多元函数微分的几何意义 多元函数偏导 那么为什么有微分和可导 能不能固定两个或者多个条件,多偏微分,哈哈
  15. VS2017的离线下载
  16. 算法工程师的能力素质模型
  17. 你知道h5游戏是什么吗,怎么制作一款h5案例?
  18. 采众家之长 凯翔软件定义存储能够后来居上吗?
  19. SAP 固定资产增值和减值
  20. Date类的getDay()和getDate()方法

热门文章

  1. 工作中搜索页面搜索记录功能的封装(存储到本地)
  2. Rabbit 安装步骤
  3. BZOJ 3534 重建
  4. Android 一个apk多个ICON执行入口
  5. XML转JSON的javascript代码
  6. 修改FileZilla生成证书的有效期
  7. 三星GT-I9308 Galaxy SIII 移动定制机 root方法 (亲测可用)
  8. 云计算-从基础到应用架构系列-云计算的概念
  9. sqlserver中如何实现时间按月,日,小时分组查询
  10. 用于HTTP加密浏览的TW2.0插件