• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell中的for循环总结

Shell中的for循环总结

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell for循环,shell for循环语句,shell脚本for循环,linux shell for循环,shell中for循环等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

在shell中常用的是 for i in $(seq 10)
for i in `ls`
for i in ${arr[@]}
for i in $* ; do
for File in /proc/sys/net/ipv4/confaccept_redirects:'
for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo $File
done
echo "直接指定循环内容"
for i in f1 f2 f3 ;do
echo $i
done
echo
echo "C 语法for 循环:"
for (( i=0; i<10; i++)); do
echo $i
done
</div>
---------------------------------------------------------------------------------------------------------
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
</div>
2.使用`seq 100`
#!/bin/bash
clear
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
</div>
3.使用while
#!/bin/bash
clear
i=1
while(($i<100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done
</div>
--------------------------------------------------------------------------------------------------------
在shell用for循环做数字递增的时候发现问题,特列出shell下for循环的几种方法:
1.
for i in `seq 1 1000000`;do
echo $i
done
</div>
用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
</div>
因为本方法调用expr故运行速度会比第1,第2种慢不少不过可稍作改进,将i=`expr $i + 1`改为i=$(($i+1))即可稍作速度的提升,不过具体得看相应shell环境是否支持
4.
for i in {1..10000000;do
echo $i
done
</div>
其实选用哪种方法具体还是得由相应的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
</div>
运行结果:
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
</div>

</div>

您可能想查找下面的文章:

  • Shell中的for循环总结
  • shell中的循环语句、判断语句实例
  • shell for循环与数组应用介绍

相关文章

  • linux系统下hosts文件详解及配置
  • Shell脚本数组操作小结
  • 使用shell脚本实现ping对应IP所对应的人名(推荐)
  • 通过shell进行数学运算的多种方式
  • shell脚本学习指南[一](Arnold Robbins & Nelson H.F. Beebe著)
  • Shell脚本批量修改文件后缀名代码分享
  • 轻松掌握Linux关机重启命令
  • 神奇的shell命令行输入与输出功能介绍
  • shell脚本联合PHP脚本采集网站的pv和alexa排名
  • Linux rpm命令参数使用大全(经典)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • CentOS 6.3下给PHP添加mssql扩展模块教程
    • Shell脚本数组用法小结
    • Linux base shell重定向详解
    • 杀掉oracle在线用户脚本分享
    • shell中冒号的特殊用法分享
    • 一个ping检测告警函数代码
    • Shell中创建序列和数组(list、array)的方法
    • 浅谈ubuntu 中sudo update与upgrade的作用及区别
    • Linux shell脚本基础学习详细介绍(完整版)第1/2页
    • Shell脚本实现的阳历转农历代码分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有