shell的FOR循环应用实例
2013-08-28 13:29
302 查看
关于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
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
相关文章推荐
- shell 编程 for 循环详解及应用实例
- Shell中的循环语句for、while、until实例讲解
- 对Linux下shell编程之for循环的实例讲解
- shell for循环和数组应用
- shell for循环与数组应用介绍
- Shell笔记8——for和select循环的应用实践
- shell for循环案例:自动批量添加iptables应用端口规则 推荐
- Shell学习·for循环实例演练 推荐
- Shell中的循环语句for、while、until实例讲解
- linux shell for循环使用命令中读取到的值实例
- Shell中的循环语句for、while、until实例讲解
- Shell中的循环语句for、while、until实例讲解
- Shell 脚本之循环语句(for,while,until)以及case,select,break,continue实例详解
- Shell中的循环语句for、while、until实例讲解
- js---javaScript(循环语句的使用实例)for while do....while(转移语句): break, continue , return
- Shell入门教程:流程控制(5)for 循环
- 基于Proteus+8051汇编应用实例系列之一--单只数码管循环显示数字
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Shell 脚本应用(for、while、case语句应用)
- shell for 循环写法总结