[linux] 结构化命令-for
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的更多相关文章
- [Linux] 结构化命令 if
语法结构如下: 1. if-then语句 # if-then语句 if command #根据conmmand的退出状态码,选择执行语句 then commands fi e.g. #!usr/bin ...
- Linux 结构化命令
if -then 语句 if -then 语句有如下格式 if command then commands f i bash shell 的if语句会先运行if后面的那个命令,如果改命令的退出状态码是 ...
- linux shell脚本使用结构化命令
内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...
- 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令
许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then ...
- Shell 语法之结构化命令(流程控制)
许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...
- bash shell笔记2 结构化命令
二.使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻辑控制流,有些命令允许脚本 ...
- shell的结构化命令
shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...
- bash shell笔记3 结构化命令二
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/513601 三 ...
- 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...
随机推荐
- iOS各种调试技巧豪华套餐
转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...
- GPU深度发掘(一)::GPGPU数学基础教程
作者:Dominik Göddeke 译者:华文广 Contents 介绍 准备条件 硬件设备要求 软件设备要求 两者选择 初始化OpenGL GLUT OpenGL ...
- IA32寄存器与x86-64寄存器的区别
IA32寄存器 一个IA32CPU包含一组8个存储32位值的通用寄存器,这些寄存器用来存储整数数据和指针: 31-0 15-0 15-8 7-0 使用惯例 %eax %ax %ah %al 调用者保存 ...
- linux 学习 12 服务管理
第十二讲 Linux服务管理 12.1 Linux服务管理-服务分类 ——Linux服务 ----RPM包默认安装的服务 ————独立的服务 ————基于xinetd服务 ----源码包安装的服务 ...
- sqlite数据库安装配置
一.SQLite简介 SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中, ...
- Office 365 系列四 ------ 绑定公司域名
Office 365包含了企业邮箱服务(Exchange Online),我们如果要用微软的企业邮箱,那么我们必须绑定我们公司的自己域名,而不是用微软 提供的二级域名,其实微软的整个Exchange ...
- .Net 零星小知识
1. 深拷贝和浅拷贝 单纯讲这两个词,其实不容易记住区别,但是看看他们对应的英语单词就显而易见了: 深拷贝: Clone 浅拷贝: Copy 记住了这个,下面在看看详细一点的信息: Copy: 只是复 ...
- 初学c# -- 学习笔记(五) winfrom自定义滚动条
找了些例子,要么庞大.要么搞个安装组件什么的,我要求能用就行了.实在找例子修改麻烦,就做了一个.其实实现挺简单,就是panel或图片什么的跟着鼠标走就行了. 这里panel自己可以加背景图或直接搞个图 ...
- HashMap的两种实现方式
本文主要简要分析了Java中和Redis中HashMap的实现,并且对比了两者的异同 1.Java的实现 下图表示了Java中一个HashMap的主要实现方式 因为大家对于Java中HashMap的实 ...
- 浅谈Java 8的函数式编程
函数式编程语言是什么? 函数式编程语言的核心是它以处理数据的方式处理代码.这意味着函数应该是第一等级(First-class)的值,并且能够被赋值给变量,传递给函数等等.(转载自http://xz.p ...