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 ...
随机推荐
- stand up meeting 12/23/2015
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 基本完成单词本显示页面的设计和实现 4 完善页面切换 ...
- Bi-shoe and Phi-shoe LightOJ - 1370
欧拉函数. 欧拉函数打表模板: #define maxn 3000010 int p[maxn]; void oula(){ int i,j; ; i<=maxn; i++) p[i]=i; ; ...
- 今天探究的CSS属性是box-sizing;
首先BOX-SIZING属性是CSS3的属性: 语法: box-sizing : content-box || border-box || inherit 取值说明 1.content-box:此值为 ...
- tensorflow版线性回归
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf def linearregression(): X ...
- Inno Setup 添加版权信息
[Setup]AppCopyright=Copyright (C) - My Company, Inc. 有以上一句,即可在右键 --> Property --> Details 里看见版 ...
- Enjoy the game
这真的只是一道规律题 我找到规律了但是因为数据太大了我超时了 我们现在来看一下这道题 牛牛战队的三个队员在训练之余会自己口胡了一些题当做平时的益智游戏.有一天牛可乐想出了一个小游戏给另外两名队员玩,游 ...
- Node 接入阿里云实现短信验证码
本文介绍在案例云开通短信服务的流程以及在Node项目中使用的方法. 一.开通阿里云短信服务 登陆阿里云,然后进入 https://dysms.console.aliyun.com/dysms.htm ...
- 微软开放 Build 2020 免费注册
微软已经开放 Build 2020 线上开发者会议注册,https://mybuild.microsoft.com/.Build 2020 会议将于 5 月 19 日至 20 日召开,核心内容都是围绕 ...
- Linux分类
Linux versions:http://www.cnblogs.com/sammyliu/articles/4832157.html1. Maintained by organization- D ...
- 【BUG之group_concat默认长度限制】
2019独角兽企业重金招聘Python工程师标准>>> 问题:mysql数据库使用group_concat将多个id组成字符串数组,一共200个,到160个被截断: 原因:mysql ...