1 for命令

 # for:迭代循环;默认空格为分隔符

 for var in list
do
commands
done

  1.1 读取列表中的值

#!usr/bin/bash
for test in Hello Python Student School
do
echo The next wprd is $test
done
echo The last state is $test
#一直保持最后迭代的值,除非删除(或更改)它

  1.2 读取列表中复杂的值

 # 使用转义字符(反斜线),转义特殊字符
# 使用双引号定义用到的单引号(或反之)
# 默认空格为分隔符
#!usr/bin/bash
for test in I don\'t know if "this'll" work
do
echo "word: $test"
done
# 双引号创建字符串变量,并作为一个整体出现在列表中

$cat fortest01.sh
#!usr/bin/bash
for var in "the test bash shell"
do
echo word: $var
done $sh fortest01.sh
word: the test bash shell $cat fortest01.sh
#!usr/bin/bash
for var in "the test bash shell" "the last test"
do
echo word: $var
done $sh fortest01.sh
word: the test bash shell
word: the last test

1.3 从变量中读取列表

 # 注意与直接读取列表的区别:
# 列表:
* 列表中特殊字符的转义字符
* 双引号-特殊字符和字符串变量(整体)
# 变量
* 定义字符串变量-双引号/单引号
* 字符串添加元素
* for迭代遍历变量元素
$cat varfortest02.sh
#!usr/bin/bash
list01='the first test example'
list02=" the second test example"
list03=$list02" the thrid test example"
list04=$list01$list02$list03
echo $list04
n=
for var in $list04
do
(( n++ ))
echo cycle $n: $var
done $sh varfortest02.sh
the first test example the second test example the second test example the thrid test example
cycle : the
cycle : first
cycle : test
cycle : example
cycle : the
cycle : second
cycle : test
cycle : example
cycle : the
cycle : second
cycle : test
cycle : example
cycle : the
cycle : thrid
cycle : test
cycle : example

1.4 从命令读取值

 # 反引号
* 使用文件的绝对路径,除非位于同一个目录
* 默认空格为分隔符
$cat commandfor.sh
#!usr/bin/bash
n=
for var in `cat varfortest02.sh`
do
n=$[ $n+ ]
echo line $n: $var
done $sh commandfor.sh
line : #!usr/bin/bash
line : list01='the
line : first
... ...
line : do
line : ((
line : n++
line : ))
... ...
line : $var
line : done

1.5 更改字段分隔符

 # 环境变量IFS-内部字段分隔符
* 默认分隔符:空格;制表符;换行符
* 更改:单个:IFS=$'\n'-(换行)
多个:IFS=$'\n:;"'-(换行/冒号/分号/双引号)
* 保存与恢复:
IFS.OLD=$IFS
IFS=$'\n'
...
IFS=$IFS.OLD $cat commandfor.sh
#!usr/bin/bash
n=
IFS.OLD=$IFS
IFS=$'\n'
for var in `cat varfortest02.sh`
do
n=$[ $n+ ]
echo line $n: $var
done
IFS=$IFS.OLD $sh commandfor.sh
commandfor.sh: line : IFS.OLD=: command not found
line : #!usr/bin/bash
line : list01='the first test example'
line : list02=" the second test example"
line : list03=$list02" the thrid test example"
line : list04=$list01$list02$list03
line : echo $list04
line : n=
line : for var in $list04
line : do
line : (( n++ ))
line : echo cycle $n: $var
line : done

1.6 用通配符读取文件/目录

 # 文件/目录变量尽量用双引号括起来
# 文件/目录查找方法和列表方法合并进同一个for语句
    * 可以添加任意多个通配符,for语句先匹配文件或目录形成列表,然后遍历列表
$cat filefor.sh
#!usr/bin/bash
for file in ./* ./tsttst
do
if [ -e "$file" ]
then
echo The file is $file
else
echo The fiel $file do not exist!
fi
done $sh filefor.sh
The file is ./commandfor.sh
The file is ./filefor.sh
The file is ./fortest01.sh
The file is ./varfortest02.sh
The fiel ./tsttst do not exist!

[linux] 结构化命令-for的更多相关文章

  1. [Linux] 结构化命令 if

    语法结构如下: 1. if-then语句 # if-then语句 if command #根据conmmand的退出状态码,选择执行语句 then commands fi e.g. #!usr/bin ...

  2. Linux 结构化命令

    if -then 语句 if -then 语句有如下格式 if command then commands f i bash shell 的if语句会先运行if后面的那个命令,如果改命令的退出状态码是 ...

  3. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

  4. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  5. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

  6. bash shell笔记2 结构化命令

    二.使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻辑控制流,有些命令允许脚本 ...

  7. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  8. bash shell笔记3 结构化命令二

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/513601 三 ...

  9. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

随机推荐

  1. Mybatis分页插件

    mybatis配置 <!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pageh ...

  2. hive中拉链表

    在有些情况下,为了保持历史的一些状态,需要用拉链表来做,这样做目的在可以保留所有状态的情况下可以节省空间. 拉链表适用于以下几种情况吧 数据量有点大,表中某些字段有变化,但是呢变化的频率也不是很高,业 ...

  3. scala-尾递归

    ------------------------- by chenkh ----------------------------- 随笔记录什么是尾递归,为什么需要尾递归,尾递归show by exa ...

  4. 并发下常见的加锁及锁的PHP具体实现-转载

    并发下常见的加锁及锁的PHP具体实现 http://www.cnblogs.com/scotoma/archive/2010/09/26/1836312.html 在最近的项目中有这样的场景 1.生成 ...

  5. SQL server 2005中的锁(1)

    在之前的一片随笔中,简单的说了一下SQL Server中的隔离级别.而SQL Server的隔离级别是通过锁的机制来实现的.现在深入一下,谈谈SQL Server中的锁. 开始之前,先要定义一下前提: ...

  6. [原创]Lodop打印, 以及Lodop引用css文件控制打印样式的问题.

    最近在做Lodop打印功能: 思路是:  用MasterPage搭个打印页面的框架, 然后在具体的页面中填入数据, 打印的样式由母版页和CSS来控制. 困扰了一天的问题是:  在打印的JS文件中, 引 ...

  7. information_schema.TABLES

    获取所有表结构(TABLES) SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA='数据库名'; TABLES表:提供了关于数据库中 ...

  8. Ubuntu 16.10下的eclipse

    下载后解压,放到文件下,打开此文件左边齿轮状的东西,即可指引安装:

  9. CC1310之使用SMARTRF STUDIO

    SMARTRF STUDIO是TI提供的射频测试软件,在调射频的时候非常非常非常好用,推荐每一个使用TI射频芯片的工程师都要掌握. 1 如何使用? 要使用SMARTRF STUDIO,硬件必须连接仿真 ...

  10. MongoDB Replica Set 选举过程

    什么是选举? 选举是副本集选择某个成员成为primary的过程.primary是一个副本集中唯一能够接收写操作的成员. 下面的事件能够引发一次选举: 第一次初始化一个副本集 Primary失效.rep ...