shell 循环结构
前言
循环结构在编程中十分常见,也是程序中是较为重要的一部分,在bash中有 for,until,while 这三种语句可以进行重复执行部分程序流程,下面会进一步讨论这三个指令的使用以及注意事项
for
bash中for命令允许用户创建遍历一个系列值的循环,在循环中,建议执行预先设定好的程序或命令。for的基本格式如下:
for val in list
do
#TODO
#commands
done
下面写几个简单的脚本熟悉一下for的用法。
1 从列表中读取
#!/bin/bash
for i in A B C D E F G
do
echo "i is: $i"
done
i is: A
i is: B
i is: C
i is: D
i is: E
i is: F
i is: G
2 从命令中读取
#!/bin/bash
val=`ls -l /`
for files in $val
do
echo "output: $files"
done
这里将根目录下的文件列表已经文件属性打印出来,由于默认分隔符号是空格,所以打印的结果如下,由于内容较多,已经省略大部分内容;
output: 总用量
output: 108
output: drwxr-xr-x
output: 2
output: root
output: root
output: 4096
output: 6月
output: 24
output: 20:53
output: bin
output: drwxr-xr-x
output: 4
output: root
output: root
output: 4096
output: 8月
output: 26
output: 06:28
output: boot
output: drwxrwxr-x
…
3 自定义分隔符
使用环境变量IFS可以将分隔符定义为用户想要使用的分隔符;
#!/bin/bash
val=`ls -l /`
#将分隔符号换成换行符
IFS=$'\n'
for files in $val
do
echo "output: $files"
done
output: 总用量 108
output: drwxr-xr-x 2 root root 4096 6月 24 20:53 bin
output: drwxr-xr-x 4 root root 4096 8月 26 06:28 boot
output: drwxrwxr-x 2 root root 4096 6月 24 00:07 cdrom
output: drwxr-xr-x 19 root root 4220 8月 25 20:23 dev
…
4 双括号下的for命令
bash shell 中可以使用C语言风格的for命令;下例简单实现了求1+2+3+…+100的和。
#!/bin/bash
sum=0
for (( i=0; i<=100; i++ ))
do
sum=$(( $sum + $i ))
done
echo $sum
5050
while
bash shell 中的while命令会测试判断当前的cmd是否返回正确值,当前cmd是否成立,如果成立,则执行循环体内的命令,while命令的基本格式如下:
while test cmd
do
#TODO
#commands
done
通过while简单实现1至100的求和公式;
#!/bin/bash
i=0
sum=0
while [ $i -le 100 ]
do
sum=$(($i+$sum))
i=$(( $i+1 ))
done
echo $sum
5050
until
until命令与while命令恰恰相反,当cmd命令不成立的时候,则执行循环体内部的指令,until命令的基本格式如下:
until test cmd
do
#TODO
#commands
done
通过until简单实现1至100的求和公式;
#!/bin/bash
i=0
sum=0
until [ $i -gt 100 ]
do
sum=$(($i+$sum))
i=$(($i+1))
done
echo $sum
shell 循环结构的更多相关文章
- For,while,case,shell循环结构
For,while,case,shell循环结构 案例1:使用for循环结构 案 ...
- shell循环结构解析:for/while/case
1.for循环结构 for var in item1 item2 ... itemN do command1 command2 ... commandN done 例如,顺序输出当前列表中的数字: # ...
- shell的编程结构体(函数、条件结构、循环结构)
bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.1 shell函数 在shell中,函数可以被当作命令一样 ...
- shell脚本--循环结构
shell的循环结构有while和for两种 for循环 #!/bin/bash #文件名:test.sh i=4 for i in 2 4 6 8 10 do echo $i done echo $ ...
- shell条件控制和循环结构
一.简介 Shell编程中循环命令用于特定条件下决定某些语句重复执行的控制方式,有三种常用的循环语句:for.while和until.while循环和for循环属于“当型循环”,而until属于“直到 ...
- Linux shell for循环结构
Linux Shell for循环结构 循环结构 1:循环开始条件 2:循环操作 3:循环终止的条件 shell语言 for,while ...
- Shell基础(三):使用for循环结构、使用while循环结构、基于case分支编写脚本、使用Shell函数、中断及退出
一.使用for循环结构 目标: 本案例要求编写一个Shell脚本chkhosts.sh,利用for循环来检测多个主机的存活状态,相关要求及说明如下: 1> 对192.168.4.0/24网段执行 ...
- shell分支与循环结构
1. 条件选择 1.1 条件判断分支介绍 格式 if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMM ...
- shell 基本结构
就像其他的编程语言一样,shell也有三种基本的结构:顺序结构.分支结构.循环结构.顺序结构就是按照命令的出现顺序依次执行,比较简单.如下分别介绍分支结构和循环结构. 分支结构 格式1: if com ...
随机推荐
- asp.net mvc 接收jquery ajax发送的数组对象
<script type="text/javascript"> $(function () { var obj = { name: "军需品", m ...
- python_ck01(虚拟环境管理)
拖拖拉拉的毛病还是依旧如初... 断断续续坚持三天总算把虚拟环境管理部分的内容给看完了. 对三天的知识点进行梳理,方便以后回顾. ①虚拟环境安装 用pip install + 包名的方式安装,涉及到的 ...
- sws_接口自动化_demo
登录接口获取token: import requests import json def get_token(username, password): host = "https://sws ...
- 多线程高并发编程(6) -- Semaphere、Exchanger源码分析
一.Semaphere 1.概念 一个计数信号量.在概念上,信号量维持一组许可证.如果有必要,每个acquire()都会阻塞,直到许可证可用,然后才能使用它.每个release()添加许可证,潜在地释 ...
- [linux] linux的top命令参数详解
简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...
- 微信小程序画布(1)
wxml: <view catchtouchmove="preventTouchMove" wx:if="{{canvas_haoBao}}"> ...
- AOP-SheepAspect
转载https://www.cnblogs.com/InCsharp/p/5902133.html SheepAspect 简介以及代码示列: SheepAspect是一个AOP框架为.NET平台,深 ...
- Python 中取代 Printf 大法的工具
「printf 大法」大概是最早期学到的 debug 方式?不同语言有不同的指令,在 Python 里对应的是 print指令 (加上%或是.format). 刚刚看到「 cool-RR/pysnoo ...
- 0day学习笔记(2)--函数调用
函数调用过程 调用函数操作 函数参数入栈(在当前函数栈帧),从左至右或从右至左视情况而定 一般为从右至左 mov 地址,参数 的一个操作并不直接pop而是定位到地址将参数传递进去 call offse ...
- centos7与8的区别
1.关于内核版本:RHEL8采用4.18.0-xRHEL7采用3.10-0-x 2 网络时间同步 RHEL8 只使用Chronyd,不支持NTP部署. RHEL7Chronyd与NTP两者都支持 3. ...