手机版
你好,游客 登录 注册
背景:
阅读新闻

Shell for循环

[日期:2015-09-12] 来源:Linux社区  作者:Linux [字体: ]

与其他编程语言类似,Shell支持for循环。

for循环一般格式为:
for 变量 in 列表
do
    command1
    command2
    ...
    commandN
done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。

in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

例如,顺序输出当前列表中的数字:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

运行结果:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5

顺序输出字符串中的字符:

for str in 'This is a string'
do
    echo $str
done

运行结果:
This is a string

显示主目录下以 .bash 开头的文件:

#!/bin/bash
for FILE in $HOME/.bash*
do
  echo $FILE
done

运行结果:
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-09/122934.htm

linux
相关资讯       Shell for  Shell循环 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款