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

1、 for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
2、在shell中常用的是 for i in $(seq 10)
3、for i in `ls`

4、for i in ${arr[@]}
5、for i in $* ; do
6、for File in /proc/sys/net/ipv4/confaccept_redirects:'
18.for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
19.echo $File
20.done
21.echo "直接指定循环内容"
22.for i in f1 f2 f3 ;do
23.echo $i
24.done
25.echo
26.echo "C 语法for 循环:"
27.for (( i=0; i<10; i++)); do
28.echo $i
29.done

---------------------------------------------------------------------------------------------------------

shell中for循环用法
shell语法好麻烦的,一个循环都弄了一会 ,找了几个不同的方法来实现输出1-100间可以被3整除的数
1.用(())
#!/bin/bash
clear

for((i=1;i<100;i++))
for
do
if((i%3==0))
then
echo $i
continue
fi
done
2.使用`seq 100`
#!/bin/bash
clear

for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
3.使用while
#!/bin/bash
clear

i=1
while(($i<100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done

--------------------------------------------------------------------------------------------------------
在shell用for循环做数字递增的时候发现问题,特列出shell下for循环的几种方法:

1.

for i in `seq 1 1000000`;do

echo $i

done

用seq 1 10000000做递增,之前用这种方法的时候没遇到问题,因为之前的i根本就没用到百万(1000000),因为项目需要我这个数字远大于百万,发现用seq 数值到 1000000时转换为1e+06,根本无法作为数字进行其他运算,或者将$i有效、正确的取用,遂求其他方法解决,如下

2.

for((i=1;i<10000000;i++));do

echo $i

done

3.

i=1

while(($i<10000000));do

echo $i

i=`expr $i + 1`

done

因为本方法调用expr故运行速度会比第1,第2种慢不少不过可稍作改进,将i=`expr $i + 1`改为i=$(($i+1))即可稍作速度的提升,不过具体得看相应shell环境是否支持

4.

for i in {1..10000000;do

echo $i

done

其实选用哪种方法具体还是得由相应的shell环境的支持,达到预期的效果,再考虑速度方面的问题。

示例:

# !/bin/sh

i=1

function test_while(){

i=1

while [ $i ]

do

echo $i

i=`expr $i + 1`

if [ $i -ge 10 ]; then

break

fi

done

}

function test_for(){

i=1

for ((i=1; i<=100; i++)); do

echo $i

if [ $i -ge 10 ]; then

break

fi

done

}

function test_continue(){

i=1

for i in $(seq 100); do

if (( i==0 )); then

echo $i

continue

fi

done

}

echo "test_while..."

test_while

echo "test_for..."

test_for

echo "test_continue..."

test_continue

运行结果:

test_while...

1

2

3

4

5

6

7

8

9

test_for...

1

2

3

4

5

6

7

8

9

10

test_continue...

10

20

30

40

50

60

70

80

90

100

shell中for循环总结的更多相关文章

  1. shell中的循环

    shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...

  2. shell中for循环

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

  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. Eclipse 中Alt+/快捷键失效的解决办法。

    1.Eclipse下进入Windows ->Preperences ->General ->keys2.把word completion的快捷键设置alt+/删掉! 3.把Conte ...

  2. uva 10859

    刘书例题  树形dp #include <cstdio> #include <cstdlib> #include <cmath> #include <map& ...

  3. 管道命令xargs

    在研究hadoop的过程中,遇到一个小难题. 问题描述:我需要将文件夹A下的文件拷贝到文件夹B中,但是以hadoop开头的文件不要拷贝. 问题解决:ls  A|grep -v hadoop|xargs ...

  4. How do you design object oriented projects?

    what are things you do during the high level design phase (before you begin programming) to determin ...

  5. POJ1013Counterfeit Dollar

    这个题主要是判断硬币真假,可能轻可能重,称三次,要输出哪枚是假币,还要输出是重的还是轻的,所以最主要的是标记变量 #include<cstdio> #include<cstring& ...

  6. 无法解析指定的连接标识符 oracle错误12154

    导出的时候老是报这个错,exp userid=c##yh/yh@MyOracle tables=(stu3) file=d:\e.dmp; 解决了好久,最后都失败了,后来加了127.0.0.1:152 ...

  7. 【POJ3243】拓展BSGS(附hash版)

    上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...

  8. hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String

    hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just  String http://www.bnuoj.co ...

  9. Android中自定义Checkbox

    custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...

  10. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...