循环

一、whlie和until循环

while循环基本语法:

while <条件表达式>
do
指令
done
#注意代码缩进

util循环基本语法:

until <表达式>
do
指令
done
#与while的区别:不成立时进入循环,成立时终止循环

注意while条件表达式跟if的区别:if后的表达式是形如'[ a -eq b ]'这样的表达式,此时返回0为真,while后跟(( a < b)),返回1时为真。

# !/bin/bash
sum=
i=
while ((sum>i)) #正确
do
((sum=sum-i))
echo "The sum now is $sum"
done
echo "The money is less than $i pls add it"
# !/bin/bash
sum=
i=
while [ $sum -gt $i ] #错误的写法
do
((sum=sum-i))
echo "The sum now is $sum"
done
echo "The money is less than $i pls add it"

使用while按行读文件:

方法一:

exec <FILE
sum=
while read line
do
echo $line
done

方法二:使用cat读文件

cat filePath | while read line
do
cmd
done

方法三:结尾重定向

while read line
do
cmd
done<$

二、for与select循环

for循环语法:

语法一:

for 变量名 in 变量取值列表
do
cmd
done

注意:"in 变量取值列表"可以省略,省略时相当于"in $@"

语法二(与C语言相识):

for((expr1;expr2;expr3))
do
cmd
done

例子:

for num in
do
echo $num
done

等同于

for num in {..}
do
echo $num
done

批量更改当前目录下的文件名:

# !/bin/bash
for n in `ls`
do
mv $n `echo $n|cut -d . -f1`.gif
done

select 循环语句主要用来打印菜单

# !/bin/bash
select name in tang jia pi bear
do
echo $name
done

in后面也可以接命令结果或者数组

Shell脚本笔记(八)循环的更多相关文章

  1. shell脚本之for循环

    shell脚本之for循环 author :headsen  chen       2017-10-18    09:50:41 个人原创,转载请注明.否则依法追究法律责任 1,cat forloop ...

  2. shell 脚本中所有循环语法

    写出 shell 脚本中所有循环语法 for 循环 : for i in $(ls);do echo item:$i done while 循环 : #!/bin/bash COUNTER=0 whi ...

  3. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  4. shell脚本中select循环语句用法

    shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...

  5. shell脚本进阶之循环判断

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  6. shell脚本笔记(原创不断记录)

    今天开始自己的shell脚本练习,刚好公司有太服务器,要时间对数据的cp是按月的: 考虑:首先寻找规律,发现都放置在/opt/www/aaa/  里面有很多的2级和3级目录和文件,但我追踪要备份的是年 ...

  7. Shell脚本笔记

      如何查询文件里的某个字符串? grep “字符串” 文件 例:grep "abc" tmp.txt   如何将查询出来的内容赋给变量? str=$(grep "abc ...

  8. Shell脚本笔记(七)控制Shell脚本

    控制Shell脚本 一.处理信号 1) SIGHUP本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联. ...

  9. Shell脚本笔记(一)一些零碎的基础知识

    一些零碎的基础知识 一.认识Shell脚本 一)相关概念 Shell是一种命令解释器,作用是按次序执行(遇到子脚本,先执行子脚本的命令)用户输入的命令和程序. Shell脚本语言是弱类型语言,与其他脚 ...

  10. Shell脚本笔记(二)Shell变量

    Shell变量 一)全局环境变量 全局变量对于定义它的shell和其子shell都是可见的,但如果生成它的shell被终止,全局变量也就消失了.另外全局变量会被子shell的同名变量覆盖. #定义一个 ...

随机推荐

  1. Tensorflow 中的优化器解析

    Tensorflow:1.6.0 优化器(reference:https://blog.csdn.net/weixin_40170902/article/details/80092628) I:  t ...

  2. PyOpenCV图像逆时针旋转90度

    warpAffine方法效果很搓,留下大片黑色区域. 使用flip和transpose可以实现逆时针旋转90度.先flip或先transpose均可. #coding:utf-8 import cv2 ...

  3. C++11 中的function和bind、lambda用法

    std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...

  4. How to disable Microsoft Compatibility Telemetry

    Issue: How to disable Microsoft Compatibility Telemetry (CompatTelRunner.exe)?   Option : Disable Mi ...

  5. fcagte.exe应用程序错误

    原文:What is Fcagte.exe and How To Fix It? Overview of Fcagte.exe What Is Fcagte.exe? Fcagte.exe is a ...

  6. 494. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  7. The authenticity of host 'slaver2 (192.168.199.132)' can't be established. RSA key fingerprint is cc:4e:23:01:ca:97:52:21:85:78:bc:29:ca:b3:12:52.

    1:ssh登录 The authenticity of host 192.168.199.132 can't be established. 的问题 问题出现了,总要解决吧,百度一下,详细介绍的很多, ...

  8. [转]笔记本怎么设置WIfi热点

    https://jingyan.baidu.com/article/335530da4f774019cb41c3eb.html 随着手机的发展,流量的消耗也是大大地增加.虽然很多手机支持wifi,但是 ...

  9. nginx 域名泛解析

    部分应用场景下要求服务器根据客户输入的二级域名地址自动访问不同的页面,比如一个服务器放置了不同的业务,商城.官网等多个业务,又不想一个个配置server, 网站目录结构入戏: html 网站根目录 m ...

  10. day100-序列化组件

    3 序列化组件 class BookSerializer(serializers.Serializer): title = serializers.CharField( max_length=32) ...