[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 ...
随机推荐
- ubuntu 'Unable to correct problems, you have held broken packages' 错误
在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...
- net命令
net命令可以完成非常多的任务.通过键入 net /? 可以查看net命令的详细列表. 在所有的Windows机器上,net命令使用统一的命令集合,这对于网络管理员来说是非常方便的. 使用net命令可 ...
- ubuntu14.04 upgrade出现【Ubuntu is running in low-graphics mode】问题的一个解决办法
在ubuntu14.04上安装docker的时候,由于眼花没看清下图这句话: 直接执行了sudo apt-get upgrade命令.然后发生了一个悲剧! 重启后出现下面这个错误! 而且在点击OK进入 ...
- Centos7上安装dnf-plugins-core
为了在Centos上使用dnf copr命令,需要安装dnf-plugins-core,找了很长时间,包括试了源码编译安装都不行,最后找到了方法,执行下面的命令就可以了. 需要root权限 wget ...
- 使用用Generic.xaml加载默认的主题资源
把Resource嵌入到Generic.xaml文件中,并把该文件放到应用程序的Themes主题文件夹下面,这们Generic.xaml文件中的资源就可以被系统识别为默认主题一部分,从而进行使用. 为 ...
- WAMPP安装后mysql无法启动
上午 10:23:42 [mysql] This may be due to a blocked port, missing dependencies, 上午 10:23:42 [mysql] imp ...
- winform开发之UI系列
1.如何构造一个漂亮的主窗体 主要讲述如何对一个新建窗体的美化过程,涉及到经常需要用到的几个属性我会着重强调它的用法,并不断更新它,因为楼主也正在探索中.... 步骤如下: vs新建一个winform ...
- 使用 Entity Framework Core 时,通过代码自动 Migration
一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...
- mvc 导入excel表格
<script> $(function () { $("#Attachment").change(function () { var att = $("#At ...
- CSS3学习内容与心得
今天2005年7月9号,博客这个东西还真不懂,以前吧我不认识它,它也不认识我.没辙,汤老要我们写,就写吧. 写什么好呢?那就写今天学习的css3的一些要点吧. css这门课程几乎都学完了.而我觉得我还 ...