Linux&shell之结构化命令进阶
写在前面:案例、常用、归类、解释说明。(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之结构化命令进阶的更多相关文章
- Linux&shell之结构化命令
写在前面:案例.常用.归类.解释说明.(By Jim)使用if-then语句如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令.如果命令的退出状态是0以外的其他值,那么then后面的 ...
- shell的结构化命令
shell在逻辑流程控制这里会根据设置的变量值的条件或其他命令的结果跳过一些命令或者循环执行的这些命令.这些命令通常称为结构化命令 1.if-then语句介绍 基本格式 if command then ...
- shell初级-----结构化命令
if-then语句 bash shell的if语句会执行if后面的那个命令,如果该命令的退出码状态为0会执行then部分的命令,如果是其他值不会执行. 格式如下: if command then co ...
- Shell编程—结构化命令(2)
1for命令 for命令的基本格式: for var in list do commands done 在list参数中,你需要提供迭代中要用到的一系列值. 1.1读取列表中的值 例子: $ vim ...
- Shell编程—结构化命令
1使用if-then语句 f-then语句有如下格式. if command then commands fi bash shell的if语句会运行if后面的那个命令.如果该命令的退出状态码是0(该命 ...
- 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 ...
- Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)
一.概述 在上一篇里讲到了shell脚本,shell按照命令在脚本中出现的顺序依次进行处理,对于顺序操作已经足够了,但许多程序要求对shell脚本中的命令加入一些逻辑流程控制,这样的命令通常叫做 结构 ...
- Shell 语法之结构化命令(流程控制)
许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...
随机推荐
- Gridview实现银行选择列表
[MainActivity.java] package com.example.activitydemo; import android.os.Bundle; import android.view. ...
- caffe源代码分析--math_functions.cu代码研究
当中用到一个宏定义CUDA_KERNEL_LOOP 在common.hpp中有. #defineCUDA_KERNEL_LOOP(i,n) \ for(inti = blockIdx.x * bloc ...
- 黑马程序猿_try-catch-finally
------- android培训.java培训.期待与您交流! ---------- try-catch-finally中怎样定义语句呢? 1.try块中主要定义可能出现的异常处理语句 2.catc ...
- Valgrind 安装与使用
调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2 2. 解压安装包:tar –jxvf ...
- MongoDB C++ 2.4.5 driver 编译安装问题
安装参考前文,http://blog.csdn.net/sheismylife/article/details/8794589 方法一致.只不过这次在GCC4.8.1上编译. scons instal ...
- 【iOS】3D Touch
文章内容来源于Apple的开发者文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptu ...
- css中var函数
引言: 在学习elementui的时候看到一个var.css, 其中写的全部都是以--开头的属性,上google查询不是css3新增的属性,于是决定一探究竟 :root { /* Transition ...
- warning:1071 (42000) Specified key was too long;max key length is 1000 bytes
原因是mysql字段长度设置的太长了, 从而导致mysql在建立索引时,索引长度超过了mysql默认许可的长度 默认 Innodb 允许长度为 767 MyISAM 允许长度为 1000 官方说明 如 ...
- The following module was built either with optimizations enabled or witherout debug information
出现这个问题的原因是这个程式有做版控,服务器上的版本比本机版本小 解决方式为:删除服务器上的版控或者本机版本改成与服务器一致即可
- 在eclipse中新建Dynamic web project时选择2.5和3.0的区别(里面涉及servlet和tomcat的问题)
1.是指servlet的版本,是2.5的还是3.0的 servlet3.0以后支持异步 2.dynamic web module和对应的TOMCAT 版本 http://blog.sina.com.c ...