常用的linux技巧,Linux Shell常用技巧(十二)-第二部分

下面的示例使用了let命令操作符,如:

/> cat

> test3.shif (( $# != 2

))#等同于

[ $# -ne 2

]

thenecho "Usage: $0 arg1 arg2"

1>&2exit 1

#exit退出值为0-255之间,只有0表示成功。

fiif (( $1 < 0 || $1

> 30 ))#等同于

[ $1 -lt 0 -o $1 -gt 30

]

thenecho "arg1 is out of range."exit 2fiif (( $2 <= 20

))#等同于

[ $2 -le 20

]

thenecho "arg2 is out of range."fiCTRL+D/> sh

./test3.shUsage:

./test3.sh arg1 arg2

/> echo

$?#Shell脚本的退出值为exit的参数值。1

/> sh ./test3.sh 40

30arg1 is out

of range.

/> echo

$?2下面的示例为如何在if的条件表达式中检验空变量:

/> cat

> test4.shif [ "$name" = ""

]#双引号就表示空字符串。

thenecho "name is null."fiCTRL+D/> .

./test4.shname is

null.if/elif/else语句的使用方式和if语句极为相似,相信有编程经验的人都不会陌生,这里就不在赘述了,其格式如下:if command

then

command

elif command

then

command

else

command

fi见如下示例脚本:

/> cat

> test5.shecho -e "How old are you?

\c"read ageif [ $age -lt 0 -o $age -gt 120

]#等同于

(( age < 0 || age

> 120 ))

thenecho "You are so old."elif [ $age -ge 0 -a $age -le 12

]#等同于

(( age >= 0

&& age <= 12

))

thenecho "You are child."elif [ $age -ge 13 -a $age -le 19

]#等同于

(( age >= 13

&& age <= 19

))

thenecho "You are 13--19 years old."elif [ $age -ge 20 -a $age -le 29

] #等同于

(( age >= 20

&& age <= 29

))

thenecho "You are 20--29 years old."elif [ $age -ge 30 -a $age -le 39

] #等同于

(( age >= 30

&& age <= 39

))

thenecho "You are 30--39 years old."elseecho "You are above 40."fiCTRL+D/> .

./test5.shHow old are

you? 50You are

above 40.case语句格式如下:case variable in

value1)

command

;;

#相同于C语言中case语句内的break。

value2)command

;;

*)

#相同于C语言中switch语句内的default

command

;;

esac见如下示例脚本:/> cat >

test6.sh#!/bin/shecho -n "Choose a color: "read colorcase "$color" in[Bb]l??)echo "you select blue color.";;[Gg]ree*)echo "you select green color.";;red|orange)echo "you select red or orange.";;*)echo "you select other color.";;esacecho "Out of case

command."/> .

./test6.shChoose a

color: greenyou select

green color.

Out of case

command.4. 循环语句:Bash

Shell中主要提供了三种循环方式:for、while和until。for循环声明格式:for variable in word_list

do

command

done见如下示例脚本:

/> cat

> test7.shfor score in math english physics

chemist #for将循环读取in后面的单词列表,类似于Java的for-each。doecho "score = $score"doneecho "out of for loop"CTRL+D

/> .

./test7.shscore =

math

score =

english

score =

physics

score =

chemist

out of for

loop

/> cat

> mylist#构造数据文件

tompattyannjakeCTRL+D

/> cat

> test8.sh#!/bin/shfor person in $(cat

mylist)#for将循环读取cat mylist命令的执行结果。

doecho "person = $person"doneecho "out of for loop."CTRL+D/> .

./test8.shperson =

tom

person =

patty

person =

ann

person =

jake

out of for

loop.

/> cat

> test9.shfor file in

test[1-8].sh#for将读取test1-test8,后缀为.sh的文件

doif [ -f $file ]#判断文件在当前目录是否存在。

thenecho "$file

exists."fidoneCTRL+D/> .

./test9.shtest2.sh

exists.

test3.sh

exists.

test4.sh

exists.

test5.sh

exists.

test6.sh

exists.

test7.sh

exists.

test8.sh

exists.

/> cat

> test10.shfor name in

$*#读取脚本的命令行参数数组,还可以写成for name的简化形式。

doecho "Hi, $name"doneCTRL+D/> . ./test10.sh stephen

annHi,

stephen

Hi,

annwhile循环声明格式:

while command#如果command命令的执行结果为0,或条件判断为真时,执行循环体内的命令。do

command

done见如下示例脚本:

/> cat

>

test1.shnum=0while (( num < 10

))#等同于

[ $num -lt 10

]

doecho -n "$num "let num+=1doneecho -e "\nHere's out of

loop."CTRL+D/> .

./test1.sh0 1 2 3 4 5

6 7 8 9

Here's out

of loop.

/> cat

> test2.shgo=startecho Type q to quit.while [[ -n $go

]]#等同于[ -n "$go" ],如使用该风格,$go需要被双引号括起。

doecho -n How are you.read wordif [[ $word == [Qq] ]]#等同于[ "$word" = Q -o "$word" = q

]

thenecho Bye.go=

#将go变量的值置空。fidoneCTRL+D/> .

./test2.shHow are

you. HiHow are

you. qBye.until循环声明格式:

until command #其判断条件和while正好相反,即command返回非0,或条件为假时执行循环体内的命令。

docommand

done见如下示例脚本:

/> cat

> test3.shuntil who | grep

stephen#循环体内的命令将被执行,直到stephen登录,即grep命令的返回值为0时才退出循环。

dosleep 1echo "Stephen still doesn't login."doneCTRL+Dshift命令声明格式:shift [n]shift命令用来把脚本的位置参数列表向左移动指定的位数(n),如果shift没有参数,则将参数列表向左移动一位。一旦移位发生,被移出列表的参数就被永远删除了。通常在while循环中,shift用来读取列表中的参数变量。见如下示例脚本:

/> set stephen ann

sheryl mark #设置4个参数变量。

/>

shift#向左移动参数列表一次,将stephen移出参数列表。

/> echo

$*ann sheryl

mark

/> shift

2#继续向左移动两位,将sheryl和ann移出参数列表

/> echo

$*mark

/> shift

2#继续向左移动两位,由于参数列表中只有mark了,因此本次移动失败。

/> echo

$*mark

/> cat

> test4.shwhile (( $# > 0

)) #等同于

[ $# -gt 0

]

doecho $*shiftdoneCTRL+D/> . ./test4.sh a b c d

ea b c d

e

b c d

e

c d e

d e

ebreak命令声明格式:break [n]和C语言不同的是,Shell中break命令携带一个参数,即可以指定退出循环的层数。如果没有指定,其行为和C语言一样,即退出最内层循环。如果指定循环的层数,则退出指定层数的循环体。如果有3层嵌套循环,其中最外层的为1,中间的为2,最里面的是3。见如下示例脚本:

/> cat

> test5.shwhile truedoecho -n "Are you ready to move on?"read answerif [[ $answer == [Yy] ]]thenbreakelseecho "Come on."fidoneecho "Here we are."CTRL+D/> .

./test5.shAre you

ready to move on? yHere we

are

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值