博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[linux] 结构化命令-for
阅读量:7012 次
发布时间:2019-06-28

本文共 3474 字,大约阅读时间需要 11 分钟。

1 for命令

1 # for:迭代循环;默认空格为分隔符2 3 for var in list4 do5     commands6 done

  1.1 读取列表中的值

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

  1.2 读取列表中复杂的值

1 # 使用转义字符(反斜线),转义特殊字符2 # 使用双引号定义用到的单引号(或反之)3 # 默认空格为分隔符4 #!usr/bin/bash5 for test in I don\'t know if "this'll" work6 do7     echo "word: $test"8 done
# 双引号创建字符串变量,并作为一个整体出现在列表中$cat fortest01.sh#!usr/bin/bashfor var in "the test bash shell"do        echo word: $vardone$sh fortest01.shword: the test bash shell$cat fortest01.sh#!usr/bin/bashfor var in "the test bash shell" "the last test"do        echo word: $vardone$sh fortest01.shword: the test bash shellword: the last test

1.3 从变量中读取列表

1 # 注意与直接读取列表的区别: 2     # 列表: 3         * 列表中特殊字符的转义字符 4         * 双引号-特殊字符和字符串变量(整体) 5     # 变量 6         * 定义字符串变量-双引号/单引号 7         * 字符串添加元素 8         * for迭代遍历变量元素 9 $cat varfortest02.sh10 #!usr/bin/bash11 list01='the first test example'12 list02=" the second test example"13 list03=$list02" the thrid test example"14 list04=$list01$list02$list0315 echo $list0416 n=017 for var in $list0418 do19         (( n++ ))20         echo cycle $n: $var21 done22 23 $sh varfortest02.sh24 the first test example the second test example the second test example the thrid test example25 cycle 1: the26 cycle 2: first27 cycle 3: test28 cycle 4: example29 cycle 5: the30 cycle 6: second31 cycle 7: test32 cycle 8: example33 cycle 9: the34 cycle 10: second35 cycle 11: test36 cycle 12: example37 cycle 13: the38 cycle 14: thrid39 cycle 15: test40 cycle 16: example

 1.4 从命令读取值

1 # 反引号 2         * 使用文件的绝对路径,除非位于同一个目录 3         * 默认空格为分隔符 4 $cat commandfor.sh 5 #!usr/bin/bash 6 n=0 7 for var in `cat varfortest02.sh` 8 do 9         n=$[ $n+1 ]10         echo line $n: $var11 done12 13 $sh commandfor.sh14 line 1: #!usr/bin/bash15 line 2: list01='the16 line 3: first17 ... ...18 line 24: do19 line 25: ((20 line 26: n++21 line 27: ))22 ... ...23 line 31: $var24 line 32: done

1.5 更改字段分隔符

1 # 环境变量IFS-内部字段分隔符 2         * 默认分隔符:空格;制表符;换行符 3         * 更改:单个:IFS=$'\n'-(换行) 4                多个:IFS=$'\n:;"'-(换行/冒号/分号/双引号) 5         * 保存与恢复: 6                 IFS.OLD=$IFS 7                 IFS=$'\n' 8                 ... 9                 IFS=$IFS.OLD    10 11 $cat commandfor.sh12 #!usr/bin/bash13 n=014 IFS.OLD=$IFS15 IFS=$'\n'16 for var in `cat varfortest02.sh`17 do18         n=$[ $n+1 ]19         echo line $n: $var20 done21 IFS=$IFS.OLD22 23 $sh commandfor.sh24 commandfor.sh: line 3: IFS.OLD=: command not found25 line 1: #!usr/bin/bash26 line 2: list01='the first test example'27 line 3: list02=" the second test example"28 line 4: list03=$list02" the thrid test example"29 line 5: list04=$list01$list02$list0330 line 6: echo $list0431 line 7: n=032 line 8: for var in $list0433 line 9: do34 line 10:        (( n++ ))35 line 11:        echo cycle $n: $var36 line 12: done

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

1 # 文件/目录变量尽量用双引号括起来 2 # 文件/目录查找方法和列表方法合并进同一个for语句     * 可以添加任意多个通配符,for语句先匹配文件或目录形成列表,然后遍历列表 3 $cat filefor.sh 4 #!usr/bin/bash 5 for file in ./* ./tsttst 6 do 7         if [ -e "$file" ] 8         then 9                 echo The file is $file10         else11                 echo The fiel $file do not exist!12         fi13 done14 15 $sh filefor.sh16 The file is ./commandfor.sh17 The file is ./filefor.sh18 The file is ./fortest01.sh19 The file is ./varfortest02.sh20 The fiel ./tsttst do not exist!

 

转载于:https://www.cnblogs.com/xiaofeiIDO/p/6214612.html

你可能感兴趣的文章
《版式设计——日本平面设计师参考手册》—第1章段落样式和字符样式的应用...
查看>>
《软件工艺师:专业、务实、自豪》一3.7.1 软件工艺峰会
查看>>
《善用佳软:高效能人士的软件应用之道》一2.4 项目管理:免费Project查看软件汇总...
查看>>
Galera 将死 — MySQL Group Replication 发布
查看>>
Mozilla 发现用于中间人攻击的证书
查看>>
Docker 中管理数据 【已翻译100%】
查看>>
《Unity 5.x游戏开发实战》一2.2 Unity中的C#脚本
查看>>
《OOD启思录》—第2章2.3节 类耦合与内聚
查看>>
【好书推荐】适合开发者学习DevOps的5本好书
查看>>
11个Linux基础面试问题
查看>>
《嵌入式 Linux C 语言应用程序设计(修订版)》一导读
查看>>
《Python金融大数据分析》一2.3 延伸阅读
查看>>
Android View事件传递详解
查看>>
Elasticsearch-SQL
查看>>
我的失败与伟大 —— 上市之后的规划
查看>>
【Spark Summit East 2017】不必犹豫,使用Spark 2.0结构化流
查看>>
HTTPFS: 基于HTTP操作hadoop hdfs文件系统
查看>>
使用jquery获取父元素或父节点的方法
查看>>
如何让双十一数据大屏讲出故事?设计有口诀
查看>>
浮窗系列之窗口与用户输入系统
查看>>