Shell-流程控制
一、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-流程控制相关推荐
- shell 脚本比较字符串相等_LINUX快速入门第十六章:Shell 流程控制
Shell 流程控制 和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?phpif (isset($_GET["q"])) { s ...
- Shell——流程控制
Shell 流程控制 和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) {s ...
- 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 ...
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例 --转载
http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html nux shell有一套自己的流程控制语句,其中包括条件语句(if),循环 ...
- linux shell 流程控制
导读 和Java.PHP等语言不一样,linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case).下面我将通过例子介绍下,各个语句使 ...
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case).下面我将通过例子介绍下,各个语句使用方法. 一.shell条件语句(if ...
- 3.Linux Shell流程控制
1.if/else结构 if condition thenstatements elif condition thenstatements elsestatements fi 2.条件 与C语言不同的 ...
- false shell 判断_六、Shell流程控制-if判断语句
1. shell中的运算 1.1 数学比较运算 -eq 等于 -ne 不等于 -lt 小于 -gt 大于 -le 小于等于 -ge 大于等于 浮点数的比较建议将数字等比放大至整数进行比较,例如 #比较 ...
- Shell——流程控制(if、case、for、while)
文章目录 一.if语句 1.基本语法 2.注意事项 3.示例演示 三.case语句 1.基本语法 2.注意事项 3.示例演示 二.for循环 1.基本语法 2.示例演示 四.while循环 1.基本语 ...
- Shell流程控制之until
基本语法 until condition dodosomething... done until与while正好相反,until会一直执行循环体中的命令,直到condition为true位置.cond ...
最新文章
- matlab中(1 )什么意思,matlab中area(1)什么意?mat – 手机爱问
- 【数理知识】《积分变换与场论》王振老师-第4章-矢量分析
- OpenCL-3-同步机制
- C/C++基础知识:函数指针和指针函数的基本概念
- HTML5新特征、窍门和技术(6~10)
- linux桌面隐藏鼠标,如何隐藏鼠标光标
- python优先级排序_用Python实现优先级队列的3种方法
- Java项目迁移到uap上_Tomcat启动,不能加载项目问题。
- ubuntu服务器无法运行chromedriver解决方法(转)
- systemback Linux 系统备份、迁移
- python 爬取直播_python 斗鱼直播间爬取代码
- PROTUES实例——stm32点灯
- bootstarp怎么使盒子到最右边_疯狂搞机 | 联通IPTV盒子免拆安装第三方软件
- 一元函数,多元函数,可微的含义 多元函数微分的几何意义 多元函数偏导 那么为什么有微分和可导 能不能固定两个或者多个条件,多偏微分,哈哈
- VS2017的离线下载
- 算法工程师的能力素质模型
- 你知道h5游戏是什么吗,怎么制作一款h5案例?
- 采众家之长 凯翔软件定义存储能够后来居上吗?
- SAP 固定资产增值和减值
- Date类的getDay()和getDate()方法