shell中的循环

for循环

类似于C语言的步长控制

例如:

for ((i=;i<=;i++)); do echo $(expr $i \*  ); done 

将1到10,依次乘以4,然后打印出来。

这里顺便提一下,shell里面表达式的计算,可以有其他形式:

for ((i=;i<=;i++)); do echo $((i * )); done   
for ((i=;i<=;i++)); do echo $[i *  ]; done          

类似于python的迭代序列

比如遍历一个数组:

a=(     )

for v in ${a[*]}; do
echo $v
done

上面这段代码可以用seq命令简化:

 for i in $(seq   ) ;do echo $i ;done 

打印出所有小写字母:

for i in {a..z} ;do echo $i ;done  

遍历脚本参数:

for i in $* ; do
echo $i
done

遍历当前目录下所有以.txt结尾的文件:

for f in $(ls *.txt); do
echo $f
done

可以简化为:

for f in *.txt; do
echo $f
done

另外,for...in循环会对字符串按空格分隔进行遍历:

str="hello world how do you do!"

for i in $str; do
echo $i ;
done


while循环

例如, 按行遍历一个文件:

while read line; do
#process line
done < file

shell中的循环的更多相关文章

  1. shell中for循环

    shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...

  2. shell中for循环总结

    关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...

  3. Shell中的循环语句实例

    1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:" ...

  4. (八)shell中的循环结构

    1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...

  5. Linux shell编程 4 ---- shell中的循环

    1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...

  6. shell中while循环的陷阱

    在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...

  7. shell中的循环语句

    for语法格式 for var in list;do commands done 其中list可以包含: 1) 直接写 for alpha in a b c d;do echo $alpha done ...

  8. shell中的循环语法

    shell中的循环语法              作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.for循环 1.语法格式1 for 变量 in 值1 值2 值3 ... do ...

  9. shell中的循环语句while

    循环语句的结构: ------------| while 条件        | do | 需要执行的命令   | done  | -----------| 例如: 1.while一直循环 2.whi ...

随机推荐

  1. php基础08:改变数据类型

    <?php //1.获取数据类型 $num = 55; echo gettype($num); //integer //2.设置数据类型 settype($num, "string&q ...

  2. scrapy 的 selector 练习

    网页结构: <html> <head> <base href='http://example.com/' /> <title>Example websi ...

  3. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接

    10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. 系统级IO实践学习记录

    代码分析 cp1.c 功能:复制文件. #include <stdio.h>#include <stdlib.h>#include <unistd.h>#inclu ...

  6. 20145215《Java程序设计》实验一实验报告

    实验一 Java开发环境的熟悉 实验内容及步骤 使用JDK编译.运行简单的Java程序 命令行下程序开发: 在命令行下建立实验目录,进入该目录后创建exp1目录 敲入以下代码: package exp ...

  7. [C/C++基础] C语言常用函数memset的使用方法

    函数声明:void *memset(void *s, int ch, size_t n); 用途:为一段内存的每一个字节都赋予ch所代表的值,该值采用ASCII编码. 所属函数库:<memory ...

  8. C# txt格式记录时间,时间对比,决定是否更新代码记录Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  9. MVC——应用Ajax获取不到数据问题解答

    当我们使用控制器利用Ajax获取表单数据时,调试为null,这时看看你接受表单时定义的参数名字是否为action 其实不能起这个名字的,这个名字和控制器关键字冲突了 随便换个其它名字就好了,比如我起个 ...

  10. iOS--SDAutolayout宽度自适应

    #pragma mark - UIScrollView 内容竖向自适应.内容横向自适应方法 @interface UIScrollView (SDAutoContentSize) /** 设置scro ...