SHELL学习笔记一

SHELL学习笔记二

SHELL学习笔记三

for 命令

which

until 命令
循环处理文件数据
控制循环

处理循环输出

for 命令

for var in list
do
commands
done
$ cat test1
#!/bin/bash
# basic for command for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done
$ ./test1
The next state is Alabama
The next state is Alaska
The next state is Arizona
The next state is Arkansas
The next state is California
The next state is Colorado
$

读取列表中的复杂值

  • 使用转义字符(反斜线)来将单引号转义;
  • 使用双引号来定义用到单引号的值。
$ cat test2
#!/bin/bash
# another example of how not to use the for command for test in I don\'t know if "this'll" work
do
echo "word:$test"
done
$ ./test2
word:I
word:don't
word:know
word:if
word:this'll
word:work
$

从变量读取列表

$ cat test4
#!/bin/bash
# using a variable to hold the list list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state?"
done
$ ./test4
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?
$

从命令读取值

$ cat test5
#!/bin/bash
# reading values from a file file="states" for state in $(cat $file)
do
echo "Visit beautiful $state"
done
$ cat states
Alabama
Alaska
Arizona
Arkansas
Colorado
Connecticut
Delaware
Florida
Georgia
$ ./test5
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
Visit beautiful Arkansas
Visit beautiful Colorado
Visit beautiful Connecticut
Visit beautiful Delaware
Visit beautiful Florida
Visit beautiful Georgia
$

更改字段分隔符

造成这个问题的原因是特殊的环境变量 IFS ,叫作内部字段分隔符(internal field separator)。IFS 环境变量定义了bash shell用作字段分隔符的一系列字符。默认情况下,bash shell会将下列字符当作字段分隔符:

  • 空格
  • 制表符
  • 换行符
将这个语句加入到脚本中,告诉bash shell在数据值中忽略空格和制表符。对前一个脚本使用
这种方法,将获得如下输出。
$ cat test5b
#!/bin/bash
# reading values from a file
file="states"
IFS=$'\n'
for state in $(cat $file)
do
echo "Visit beautiful $state"
done
$ ./test5b
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
Visit beautiful Arkansas
Visit beautiful Colorado
Visit beautiful Connecticut
Visit beautiful Delaware
Visit beautiful Florida
Visit beautiful Georgia
Visit beautiful New York
Visit beautiful New Hampshire
Visit beautiful North Carolina
$

警告  在处理代码量较大的脚本时,可能在一个地方需要修改 IFS 的值,然后忽略这次修改,在脚本的其他地方继续沿用 IFS 的默认值。一个可参考的安全实践是在改变 IFS 之前保存原来的 IFS 值,之后再恢复它。 这种技术可以这样实现:
  IFS.OLD=$IFS
  IFS=$'\n'
  <在代码中使用新的IFS值>
  IFS=$IFS.OLD
  这就保证了在脚本的后续操作中使用的是 IFS 的默认值。

用通配符读取目录

$ cat test6
#!/bin/bash
# iterate through all the files in a directory for file in /home/rich/test/*
do if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
$ ./test6
/home/rich/test/dir1 is a directory
/home/rich/test/myprog.c is a file
/home/rich/test/myprog is a file
/home/rich/test/myscript is a file
/home/rich/test/newdir is a directory
/home/rich/test/newfile is a file
/home/rich/test/newfile2 is a file
/home/rich/test/testdir is a directory
/home/rich/test/testing is a file
/home/rich/test/testprog is a file
/home/rich/test/testprog.c is a file
$

which

while test   command
do
other commands
done
$ cat test10
#!/bin/bash
# while command test var1=
while [ $var1 -gt ]
do
echo $var1
var1=$[ $var1 - ]
done
$ ./test10 $

使用多个测试命令
while 命令允许你在 while 语句行定义多个测试命令。只有最后一个测试命令的退出状态码会被用来决定什么时候结束循环。如果你不够小心,可能会导致一些有意思的结果。下面的例子将说明这一点。

$ cat test11
#!/bin/bash
# testing a multicommand while loop var1= while echo $var1
[ $var1 -ge ]
do
echo "This is inside the loop"
var1=$[ $var1 - ]
done
$ ./test11 This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop This is inside the loop
-
$

until 命令

until 命令和 while 命令工作的方式完全相反。 until 命令要求你指定一个通常返回非零退出状态码的测试命令。只有测试命令的退出状态码不为 0 ,bash shell才会执行循环中列出的命令。一旦测试命令返回了退出状态码 0 ,循环就结束了。

until test commands
do
other commands
done
$ cat test12
#!/bin/bash
# using the until command var1= until [ $var1 -eq ]
do
echo $var1
var1=$[ $var1 - ]
done
$ ./test12 $

循环处理文件数据

通常必须遍历存储在文件中的数据。这要求结合已经讲过的两种技术:

  • 使用嵌套循环
  • 修改 IFS 环境变量

通过修改 IFS 环境变量,就能强制 for 命令将文件中的每行都当成单独的一个条目来处理,即便数据中有空格也是如此。一旦从文件中提取出了单独的行,可能需要再次利用循环来提取行中的数据。
典型的例子是处理/etc/passwd文件中的数据。这要求你逐行遍历/etc/passwd文件,并将 IFS变量的值改成冒号,这样就能分隔开每行中的各个数据段了。

#!/bin/bash
# changing the IFS value IFS.OLD=$IFS
IFS=$'\n'
for entry in $(cat /etc/passwd)
do
echo "Values in $entry –"
IFS=:
for value in $entry
do
echo " $value"
done
done
$ Values in rich:x:::Rich Blum:/home/rich:/bin/bash -
rich
x Rich Blum
/home/rich
/bin/bash
Values in katie:x:::Katie Blum:/home/katie:/bin/bash -
katie
x Katie Blum
/home/katie
/bin/bash

控制循环

  • break 命令
  • continue 命令

break

break 命令接受单个命令行参数值:
break n
其中 n 指定了要跳出的循环层级。默认情况下, n 为 ,表明跳出的是当前的循环。如果你将n 设为 , break 命令就会停止下一级的外部循环。
$ cat test20
#!/bin/bash
# breaking out of an outer loop for (( a = ; a < ; a++ ))
do
echo "Outer loop: $a"
for (( b = ; b < ; b++ ))
do
if [ $b -gt ]
then
break
fi
echo " Inner loop: $b"
done
done
$ ./test20
Outer loop:
Inner loop:
Inner loop:
Inner loop:

continue 命令

continue 命令可以提前中止某次循环中的命令,但并不会完全终止整个循环。可以在循环内部设置shell不执行命令的条件。这里有个在 for 循环中使用 continue 命令的简单例子。

$ cat test21
#!/bin/bash
# using the continue command for (( var1 = ; var1 < ; var1++ ))
do
if [ $var1 -gt ] && [ $var1 -lt ]
then
continue
fi
echo "Iteration number: $var1"
done
$ ./test21
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
Iteration number:
$

处理循环输出

#shell会将 for 命令的结果重定向到文件output.txt中,而不是显示在屏幕上。
for file in /home/rich/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif
echo "$file is a file"
fi
done > output.txt

SHELL学习笔记三的更多相关文章

  1. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  2. [转帖][Bash Shell] Shell学习笔记

    [Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html  阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...

  3. python3.4学习笔记(三) idle 清屏扩展插件

    python3.4学习笔记(三) idle 清屏扩展插件python idle 清屏问题的解决,使用python idle都会遇到一个常见而又懊恼的问题——要怎么清屏?在stackoverflow看到 ...

  4. shell学习笔记汇总

    1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...

  5. shell 学习笔记2-shell-test

    一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...

  6. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  7. [Firefly引擎][学习笔记三][已完结]所需模块封装

    原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读:        笔记三主要就是各个模块的封装了,这里贴 ...

  8. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

  9. JSP学习笔记(三):简单的Tomcat Web服务器

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

随机推荐

  1. phpQuery的使用

    前言 为什么使用phpQuery phpQuery是基于php5新添加的DOMDocument.而DOMDocument则是专门用来处理html/xml.它提供了强大的xpath选择器及其他很多htm ...

  2. Python 关于super在多继承中的解析

    一.单继承情况: 解析: 创建B类的实例化对象时,执行初始化函数: 打印输出Enter B,当遇到super()调用父类初始化函数(此时是调用B类的父类A的__init__函数),输出Enter A. ...

  3. bzoj 4747: [Usaco2016 Dec]Counting Haybales

    23333,在扒了一天题解之后发现我竟然还能秒题,虽然这是个pj的sb题... (排个序,然后upper_bound和lower_bound一用就行了(是不是有O(1)的查询方法啊??貌似要离散啊,一 ...

  4. question1 赋值运算操作符

    注意的问题书上讲的很详细了 下面是代码实现,但是VS有一个问题,strcpy安全性较低,虽然可以通脱编译,但是运行会报错,提示用strcpy_s()替代,但是,这里用strcpy()替代也不行, // ...

  5. 如何使用Docker部署PHP开发环境

    本文主要介绍了如何使用Docker构建PHP的开发环境,文中作者也探讨了构建基于Docker的开发环境应该使用单容器还是多容器,各有什么利弊.推荐PHP开发者阅读.希望对大家有所帮助. 环境部署一直是 ...

  6. 玩玩负载均衡---在window与linux下配置nginx

      最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx, ...

  7. STM32学习笔记:IIC通信协议详解(附带软件模拟源码)

    什么是IIC(I2C)? IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司设计出来的一种简单.双向.二线制.同步串行总线.它是一种多向控制总 ...

  8. redis 模糊查询与删除

    创建一条数据 set  name1  zhangsan 查询 get name1 在创建一条数据 set name2 lisi 查询 get name2 模糊查询 keys name* 查询结果  n ...

  9. swing开发图形界面工具配置(可自由拖控件上去)

    swing开发图形界面工具,eclipse swing图形化操作界面工具配置 1.有一个小功能要有一个界面,之前知道有一个 图形化界面的(就是可以往上面拖控件布局的工具)JBuilder,今天上午就下 ...

  10. CSAPP读书笔记--第八章 异常控制流

    第八章 异常控制流 2017-11-14 概述 控制转移序列叫做控制流.目前为止,我们学过两种改变控制流的方式: 1)跳转和分支: 2)调用和返回. 但是上面的方法只能控制程序本身,发生以下系统状态的 ...