写在前面:案例、常用、归类、解释说明。(By Jim)

for命令
重复一系列的命令是一种常见的编程实践。

#!/bin/bash
# basic for command
for test in A B C D E F G H I J K L M N O P Q
do
echo The next letter is $test
done

结果:
The next letter is A
The next letter is B
The next letter is C
The next letter is D
The next letter is E
The next letter is F
The next letter is G
The next letter is H
The next letter is I
The next letter is J
The next letter is K
The next letter is L
The next letter is M
The next letter is N
The next letter is O
The next letter is P
The next letter is Q

每次for命令将列表中的下一个值赋值给变量test。变量$test可以像for命令语句中
使用其他脚本变量一样使用。最后一次迭代之后,变量$test在shell脚本的其他部分中仍然有效。它仍然是迭代的最后一个值。
如下:

#!/bin/bash
# basic for command
for test in A B C D
do
echo The next letter is $test
done
echo "The last letter is $test"
test=Z
echo "Wait,now the letter is $test"

结果:
The next letter is A
The next letter is B
The next letter is C
The next letter is D
The last letter is D
Wait,now the letter is Z

单引号会引起问题,如下所示

#!/bin/bash
# basic for command
for test in I don't know if this'll work
do
echo "word:$test"
done

结果:
word:I
word:dont know if thisll
word:work
由于单引号的存在出现问题了,有两种方法解决这个问题:
使用转义字符\来转义单引号;
或使用双引号来定义使用单引号的值。
两种方法都不是很好。

#!/bin/bash
# basic for command
for test in I don\'t know if this\'ll work
do
echo "word:$test"
done

#!/bin/bash
# basic for command
for test in I "don't" know if "this'll" work
do
echo "word:$test"
done

如果有的数据之间有空格,可以通过双引号括起来
如下:

#!/bin/bash
# basic for command
for test in Nevada New Hampshire New Mexico New York North Carolina
do
echo "Now going to $test"
done

结果:
Now going to Nevada
Now going to New
Now going to Hampshire
Now going to New
Now going to Mexico
Now going to New
Now going to York
Now going to North
Now going to Carolina
但是这并不是我们想要到结果,解决办法

#!/bin/bash
# basic for command
for test in Nevada "New Hampshire" "New Mexico" "New York" "North Carolina"
do
echo "Now going to $test"
done

结果:
Now going to Nevada
Now going to New Hampshire
Now going to New Mexico
Now going to New York
Now going to North Carolina

从变量读取列表

#!/bin/bash
# using a variable to hold the list
list="Beijing Shanghai Guangzhou Shenzhen Nanjing Hangzhou"
list=$list" Suzhou" for city in $list
do
echo "Have you ever visited $city"
done

结果:
Have you ever visited Beijing
Have you ever visited Shanghai
Have you ever visited Guangzhou
Have you ever visited Shenzhen
Have you ever visited Nanjing
Have you ever visited Hangzhou
Have you ever visited Suzhou

读取命令中的值
生成列表中使用的值的另一种方法是使用命令的输出。
如下:

#!/bin/bash
# reading values from a file
file="cities" for city in `cat $file`
do
echo "Have you ever visited $city"
done

文件cities中的内容
Nanjing
Shang hai
Beijing
Shenzhen
Suzhou
Guangzhou
结果:
Have you ever visited Nanjing
Have you ever visited Shang
Have you ever visited hai
Have you ever visited Beijing
Have you ever visited Shenzhen
Have you ever visited Suzhou
Have you ever visited Guangzhou

问题,这里有空格的话,就会被分成两个输出比如Shang hai 被分成了两个Shang 和hai
怎么解决呢?下面讲解
默认情况下,bash shell将下面的字符看作字段分隔符:空格,制表符,换行符
可以在shell脚本中暂时更改环境变量IFS的值。例如:

#!/bin/bash
# reading values from a file
file="cities" IFS=$'\n'
for city in `cat $file`
do
echo "Have you ever visited $city"
done

这个时候就会把空格排除在外了

使用通配符读取目录

#!/bin/bash
#iterate through all the files in a directory for file in /home/* /home/jiqing9006/shellscript/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done

结果:
/home/jiqing9006 is a directory
/home/lost+found is a directory
/home/test is a directory
/home/wghan is a directory
/home/jiqing9006/shellscript/cities is a file
/home/jiqing9006/shellscript/test1 is a file
/home/jiqing9006/shellscript/test10 is a file
/home/jiqing9006/shellscript/test11 is a file
...

C式的for命令

#!/bin/bash
#testing the C-style for loop
for((i=;i<=;i++))
do
echo "The next number is $i"
done

使用多个变量

#!/bin/bash
#testing the C-style for loop
for((a=,b=;a<=;a++,b--))
do
echo "$a-$b"
done

结果:
1-10
2-9
3-8
4-7
5-6
6-5
7-4
8-3
9-2
10-1

#!/bin/bash
#testing the C-style for loop
for((a=,b=;a<=;a++,b--))
do
echo $[ $a-$b ]
done

这样就会计算值了

While循环

#!/bin/bash
# while command test
var1=
while [ $var1 -gt ]
do
echo $var1
var1=$[ $var1 - ]
done

多条件

#!/bin/bash
# while command test
var1=
while echo $var1
[ $var1 -gt ]
do
echo "This is inside the loop"
var1=$[ $var1 - ]
done

结果
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0

until命令
until命令刚好与while命令相反。until命令需要制定一条测试命令,这条命令通常产生一个非零的退出状态。
只要测试命令的退出状态非零,bash就会执行列在循环当中的命令。一旦为0,循环停止。

#!/bin/bash
# using the until command
var1=
until [ $var1 -gt ]
do
echo $var1
var1=$[ $var1 - ]
done

将不会执行,因为符合了大于0的条件,这个与while是相反的

嵌套循环

#!/bin/bash
# nesting for loops
for((a = ;a<=;a++))
do
echo "Starting loop $a:"
for((b=;b<=;b++))
do
echo "Inside loop:$b"
done
done

(这里面空格就没那么重要了,都识别的)

结果:
Starting loop 1:
Inside loop:1
Inside loop:2
Inside loop:3
Starting loop 2:
Inside loop:1
Inside loop:2
Inside loop:3
Starting loop 3:
Inside loop:1
Inside loop:2
Inside loop:3

混合循环

#!/bin/bash
# placing a for loop inside a while loop
var1=
while [ $var1 -ge ]
do
echo "Outer loop:$var1"
for((var2=;var2<;var2++))
do
var3=$[ $var1*$var2 ]
echo "Inner loop:$var1*$var2 = $var3"
done
var1=$[$var1 -]
done

结果:
Outer loop:5
Inner loop:5*1 = 5
Inner loop:5*2 = 10
Outer loop:4
Inner loop:4*1 = 4
Inner loop:4*2 = 8
Outer loop:3
Inner loop:3*1 = 3
Inner loop:3*2 = 6
Outer loop:2
Inner loop:2*1 = 2
Inner loop:2*2 = 4
Outer loop:1
Inner loop:1*1 = 1
Inner loop:1*2 = 2
Outer loop:0
Inner loop:0*1 = 0
Inner loop:0*2 = 0

再来看一下until与while结合的,准备晕吧少年

#!/bin/bash
#using until and while loops
var1=
until [ $var1 -eq ]
do
echo "Outer loop:$var1"
var2=
while [ $var2 -lt ]
do
var3=`echo "scale=;$var1/$var2"|bc`
echo " Inner loop:$var1/$var2 = $var3"
var2=$[ $var2 + ]
done
var1=$[ $var1 - ]
done

#外循环执行三次,内循环每执行四次
结果:
Outer loop:3
  Inner loop:3/1 = 3.0000
  Inner loop:3/2 = 1.5000
  Inner loop:3/3 = 1.0000
  Inner loop:3/4 = .7500
Outer loop:2
  Inner loop:2/1 = 2.0000
  Inner loop:2/2 = 1.0000
  Inner loop:2/3 = .6666
  Inner loop:2/4 = .5000
Outer loop:1
  Inner loop:1/1 = 1.0000
  Inner loop:1/2 = .5000
  Inner loop:1/3 = .3333
  Inner loop:1/4 = .2500
控制循环
break&continue

#!/bin/bash
# breaking out of a for loop for var1 in
do
if [ $var1 -eq ]
then
break
fi
echo "number:$var1"
done
echo "The for loop is completed"

同样适用于while循环

#!/bin/bash
# breaking out of a while loop var1= while [ $var1 -lt ]
do
if [ $var1 -eq ]
then
break
fi
echo "number:$var1"
var1=$[ $var1 + ]
done
echo "The while loop is completed"

(循环到等于5的时候,就停止了)

跳出内循环

#!/bin/bash
# breaking out of an inner loop for ((a=;a<;a++))
do
echo "Outer loop:$a"
for((b=;b<;b++))
do
if [ $b -eq ]
then
break
fi
echo " Inner loop:$b"
done
done

结果:
Outer loop:1
  Inner loop:1
  Inner loop:2
  Inner loop:3
  Inner loop:4
Outer loop:2
  Inner loop:1
  Inner loop:2
  Inner loop:3
  Inner loop:4
Outer loop:3
  Inner loop:1
  Inner loop:2
  Inner loop:3
  Inner loop:4
  (跳出了内部循环,但是外部循环继续进行)

跳出外部循环
break n,默认n是1,代表跳出当前循环。如果将n设置为2,将停止外循环的下一级循环。

#!/bin/bash
# breaking out of an inner loop for ((a=;a<;a++))
do
echo "Outer loop:$a"
for((b=;b<;b++))
do
if [ $b -eq ]
then
break
fi
echo " Inner loop:$b"
done
done

结果:
Outer loop:1
  Inner loop:1
  Inner loop:2
  Inner loop:3
  Inner loop:4
  (跳出了外部循环,当然本次的循环也终止了)

continue命令

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

结果:
Iteration number:1
Iteration number:2
Iteration number:3
Iteration number:4
Iteration number:5
Iteration number:10
Iteration number:11
Iteration number:12
Iteration number:13
Iteration number:14
(循环没有终止,只是跳过了,继续下一次循环)

#!/bin/bash
# using the continue command for((a=;a<=;a++))
do
echo "Iteration $a:"
for((b=;b<=;b++))
do
if [ $a -gt ]&&[ $b -lt ]
then
continue
fi
res=$[ $a*$b ]
echo "The result of $a*$b is:$res"
done
done

(这个时候,会在a为4时或5时,的大循环中断掉b小于5的几个计算)

#!/bin/bash
# using the continue command for((a=;a<=;a++))
do
echo "Iteration $a:"
for((b=;b<=;b++))
do
if [ $a -gt ]&&[ $b -lt ]
then
continue
fi
res=$[ $a*$b ]
echo "The result of $a*$b is:$res"
done
done

(这里直接跳过a为4或5的内循环,连b大于5的内容也不显示了)

#!/bin/bash
# using the continue command for((a=;a<=;a++))
do
echo "Iteration $a:"
for((b=;b<=;b++))
do
if [ $a -gt ]&&[ $b -lt ]
then
break
fi
res=$[ $a*$b ]
echo "The result of $a*$b is:$res"
done
done

(跳出a为4或5的内循环,效果等同于continue 2)

#!/bin/bash
# using the continue command for((a=;a<=;a++))
do
echo "Iteration $a:"
for((b=;b<=;b++))
do
if [ $a -gt ]&&[ $b -lt ]
then
break
fi
res=$[ $a*$b ]
echo "The result of $a*$b is:$res"
done
done

(直接跳出之后的动作,跳出外循环也终止了)

处理循环的输出

#!/bin/bash
# using the continue command for((a=;a<=;a++))
do
echo "Iteration $a:"
for((b=;b<=;b++))
do
if [ $a -gt ]&&[ $b -lt ]
then
break
fi
res=$[ $a*$b ]
echo "The result of $a*$b is:$res"
done
done > output.txt

(会将循环得到的结果写入output.txt文档下)

Linux&shell之结构化命令进阶的更多相关文章

  1. Linux&shell之结构化命令

    写在前面:案例.常用.归类.解释说明.(By Jim)使用if-then语句如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令.如果命令的退出状态是0以外的其他值,那么then后面的 ...

  2. shell的结构化命令

    shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...

  3. shell初级-----结构化命令

    if-then语句 bash shell的if语句会执行if后面的那个命令,如果该命令的退出码状态为0会执行then部分的命令,如果是其他值不会执行. 格式如下: if command then co ...

  4. Shell编程—结构化命令(2)

    1for命令 for命令的基本格式: for var in list do commands done 在list参数中,你需要提供迭代中要用到的一系列值. 1.1读取列表中的值 例子: $ vim ...

  5. Shell编程—结构化命令

    1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...

  6. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

  7. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  8. Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)

    一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...

  9. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

随机推荐

  1. ios 打电话结束返回到应用中

    在我们做打电话这个功能时,我们常常是调用这个方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel ...

  2. [ES6] Array.find()

    Convenient method to find one item in an array, avoid writing and  for + if: let arys = [1,,5,,6] ; ...

  3. Linux学习笔记共享

    从学习到现在,已经3个月了,还有不到一个月linux课程就要结束,大概的情况如下: 预科一周,主要是学习了网络,思科的内容 linux基础课程,从无到有 linux shell 脚本 linux项目实 ...

  4. SpringMVC12拦截器

    创建登陆界面 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...

  5. 9.5noip模拟试题

    题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer.o ...

  6. javascript原型prototype的一个你不一定知道的理解

    原型和原型链的故事 相关文章: 为什么原型继承很重要 先来看看一段小代码用以引入要讲的小故事. function Foo() {}; var f1 = new Foo(); Foo.prototype ...

  7. Android 巧妙实现图片和文字布局

    之前写过一个博客是关于实现图片和文字左右或者上下布局的方法, 下面是博客的主要内容: 布局文件很简单,用来展示RadioButton的使用方法. 1 <?xml version="1. ...

  8. exp、imp简单测试

    imp 分为以下几个测试场景 imp name1/password1 file=xxxx.dmp  full=y fromuser=name2 touser=name3 场景1 name1正确.pas ...

  9. 使用PPRevealSideViewController实现侧滑效果

    使用起来还是比较简单的, 主要是几个步骤 AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchi ...

  10. 05_Smart-image通过SoftReference提高性能

    文章导读: 文件介绍了常见的图片下载开源插件smart-image, 由于移动设备硬件受限,因此Android的相关app都要考虑到性能的关系, 所以很多的第三方插件都使用到了缓存cache技术,本人 ...