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. 1.使用Entity Framwork框架常用的技术手段Code First 和Reverse Engineer Code First

    提示:VS版本2013,  Entity Framwork版本5.0.0,Mysql数据库  使用Entity FrameWork的好处就不多说,直接上手如何使用.两种形式:1.将代码映射到数据库实体 ...

  2. 初学python之urllib

    urllib.request urlopen()urllib.urlopen(url, data, proxies) :创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远 ...

  3. Appium常见问题(持续更新)

    1.Original error: Could not parse activity from dumpsys 命令行:adb shell  dumpsys cpuinfo  报:service du ...

  4. 将Excel数据导入数据库

    Excel如下,这页工作表名叫“线路” 数据库表如下 using System; using System.Collections.Generic; using System.Linq; using ...

  5. CSAPP学习笔记(第一,二章)

    1:文本文件指的是ASCII码文件,二进制文件指的是除文本文件以外,其他文件. 2:区分数据对象的唯一判别方法是数据的上下文. 3:描述一下一个hello.c文件的处理过程.首先hello.c文件我们 ...

  6. Druid监控Mybatis不显示SQL问题

    一.Web.xml增加如下配置: 1.DruidWebStatFilter.如果没有配置filter信息.session监控,web监控等不可用.没有配置 <filter> <fil ...

  7. iOS强制屏幕旋转

    /** 强制旋转屏幕为纵向 (注:这种方式 键盘不能旋转过来; iOS8.x下 UIAlterView旋转不过来  ) @return */ + (void)rotateOrientationPort ...

  8. 干货|宏巍软件之Java线程监控之旅

    宏巍软件 许向 大家好,我是上海宏巍信息技术有限公司(简称:宏巍软件)的许向,宏巍软件成立于2005年,是一家以电商ERP软件开发为主的高新技术科技型软件公司,致力于为大型网商和电子商务企业提供专业. ...

  9. Win10怎么设置始终以管理员身份运行应用程序

    第一步.对着Win10应用程序桌面图标单击鼠标右键,菜单中选择“属性”; 第二步.在程序属性对话框,点击切换到“兼容性”选项卡,找到并勾选“设置”项目下的“以管理员身份运行此程序”后,点击确定即可. ...

  10. C# DateTime转Json汇总

    DateTime转换成json的时候容易出现不想要的格式,在网上搜索了相关的解决方法copy如下: 参考http://www.newtonsoft.com/json/help/html/DatesIn ...