您的位置:首页 > 运维架构 > Shell

shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select

2013-06-26 20:19 821 查看
1、expr计算整数变量值

格式 :expr arg

例子:计算(2+3)×4的值

1、分步计算,即先计算2+3,再对其和乘
4

s=`expr 2 + 3`

expr $s \* 4

2
、一步完成计算:

expr `expr 2 + 3 ` \* 4

说明:
运算符号和参数之间要有空格分开;
通配符号(*),在作为乘法运算符时要用\、“”、‘’符号修饰

:expr 3 \* 2 expr 3 “*” 2 expr 3 ‘*’ 2

`(反引号)与键盘上的~同一个键上的符号

[fsy@localhost ~]$ s=`expr 2 + 3`

[fsy@localhost ~]$ echo $s

5

[fsy@localhost ~]$ expr $s \* 4

20

[fsy@localhost ~]$ expr `expr 2 + 3` \* 4

20

[fsy@localhost ~]$ expr 2 \* 3

6

[fsy@localhost ~]$ expr 2 "*" 3

6

[fsy@localhost ~]$ expr 2 '*' 3

6

[fsy@localhost ~]$ expr 2 * 3

expr: 语法错误

[fsy@localhost ~]$

2、let命令

格式:let arg1 [arg2 ......]

说明:

与expr命令相比,let命令更简洁直观
[ ]表示可以有多个参数,arg n (n=1,2…)
运算符与操作数据之间不必用空格分开,但表达式与表达式之间必须要用空格分开

当运算符中有<、>、&、|等符号时,同样需要用引号(单引号、双引号)或者斜杠来修饰运算符

例子:计算(2+3)×4的值

[fsy@localhost ~]$ let s=(2+3)*4

[fsy@localhost ~]$ echo $s

20

[fsy@localhost ~]$

3、for语句——坑爹的开始...... 和其他语言的for不同!

对一组参数进行一个操作
语法格式:

for 变量 in 列表
do
命令行(通常用到循环变量)
done

说明:
“列表”为存储了一系列值的列表,随着循环的进行,变量从列表中的第一个值依次取到最后一个值;
do和done之间的命令通常为根据变量进行处理的一系列命令,这些命令每次循环都执行一次;
如果“ in 列表”部分省略掉,Bash则认为是“in $@”,即执行该程序时通过命令行传给程序的所有参数的列表。

例1、自定义列表

#!/bin/bash

for var in one two three four five

do

echo ------

echo '$var is' $var

done

运行输出:

------

$var is one

------

$var is two

------

$var is three

------

$var is four

------

$var is five

例2、以命令返回值作为列表

#!/bin/bash

for var in `ls`

do

echo -----

echo $var

done

运行输出:
-----

abb

-----

abc

-----

a.out

-----

a.sh

-----

b.sh

-----

hello.c

-----

scripts

-----

例3、命令行参数指定为列表中的数值

#!/bin/bash

for var

do

echo "It's $var"

done

运行输出:
[fsy@localhost ~]$ sh c.sh a b c d

It's a

It's b

It's c

It's d

4、while语句

语法格式:

while 表达式
do
命令行
done

说明:
while循环中,只要条件为真,就执行do和done之间的循环命令;
避免生成死循环。

例如:

#!/bin/bash

num=1

while [ $num -le 10 ]

do

echo -e "\t the num is $num"

let num=num+1

done

运行输出:

the num is 1

the num is 2

the num is 3

the num is 4

the num is 5

the num is 6

the num is 7

the num is 8

the num is 9

the num is 10

5、until语句

语法格式:

unitil 表达式
do
命令行
done

说明:
until循环中,只要条件不为真,就执行do和done之间的循环命令,或者说,在until循环中,一直执行do和done之间的循环命令,直到条件为真;
避免生成死循环。

例:计算1到10的和

#!/bin/bash

sum=0

num=10

until test $num -eq 0

do

sum=`expr $sum + $num`

num=`expr $num - 1`

done

echo "sum = $sum"

运行输出

sum = 55

6、shift语句

shift语句:将变量的值依次向左传递,并形成一组新的参数值
例:位置变量当前值为:1=file1 2= file2 3=file3
执行一次shift后为:1=file2 2=file3
还可以在shift命令中指定位置变量转移的次数

shift n

例:

#!/bin/bash

while [ -n "$*" ]

do

echo $1 $2 $3 $4 $5 $6

shift

done

运行输出

[fsy@localhost scripts]$ sh b.sh 1 2 3 4 5 6 7

1 2 3 4 5 6

2 3 4 5 6 7

3 4 5 6 7

4 5 6 7

5 6 7

6 7

7

7、if语句

if 语句的一般形式 :

if 条件表达式
then #当条件为真时执行以下语句
命令列表
else #当条件为假时执行以下语句
命令列表
fi

if的测试部分利用test命令实现,也可以利用一般命令执行成功与否来判断。如果命令正常结束,返回值为0,条件测试为真; 否则返回值不为0,条件测试为假

例:

#!/bin/bash

if test -f "$1"

then

echo "$1 is an ordinary file"

else

echo "$1 is not an ordinary file"

fi

运行输出:
[fsy@localhost scripts]$ sh c.sh abb

abb is not an ordinary file

[fsy@localhost scripts]$ sh c.sh a.sh

a.sh is an ordinary file

[fsy@localhost scripts]$

8、case语句

取值后面必须为单词in,每一个模式必须以右括号结束。取值可以为变量或常数。取值检测匹配的每一个模式,一旦模式匹配,其间所有命令开始执行直至;;。执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用*号捕获该值,再接受其他输入。

[注]
1.模式字符串中可以使用通配符
2.如果一个模式字符串中包含多个模式,那么各模式之间应以竖线(|)隔开,表各模式是“或”的关系,即只要给定字符串与其中一个模式相配,就会执行其后的命令列表。
3.各模式字符串应是唯一的,不应重复出现,并且要合理安排它们的出现顺序,例如,不应将“*”作为头一个模式字符串,因为“*”可以与任何字符串匹配,若第一个出现,就不会再检查其他模式了。
4.case语句以关键字case开头,以关键字esac结束。

5.case的退出(返回)值是整个结构中最后执行的命令的退出值。若没有执行任何命令,则退出值为0.

例:

#!/bin/bash

case $1 in

1)

echo " you choice is 1";;

2)

echo " your choice is 2";;

*)

echo " your choice is others";;

esac

运行输出:
[fsy@localhost scripts]$ sh d.sh 1

you choice is 1

[fsy@localhost scripts]$ sh d.sh 2

your choice is 2

[fsy@localhost scripts]$ sh d.sh 3

your choice is others

9、break与continue

1、break:用于立即终止当前循环的执行,break命令可以使用户从循环体中退出来。
语法:break
,其中,n表示要跳出几层循环,默认值为1


2、continue:跳过循环体中在其之后的语句,会返回到本循环层的开头,进行下一次循环。
语法:continue
,其中,n表示从包含continue语句的最内层循环体向外跳到第几层循环,默认值为1,循环层数是由内向外编号。


10、函数

函数:由函数标题和函数体两部分组成。标题是函数名。函数体是函数内在的命令集合。标题名称必须唯一。变量均为全局变量,没有局部变量。
格式:
[function] 函数名() 或 [function]函数名() {
{ 命令1
命令1 …
… }
}

例:

#!/bin/bash

num=1

hello()

{

echo "hello boy~ It's our $num meeting"

let num=num+1

}

hello

hello

hello

运行输出
hello boy~ It's our 1 meeting

hello boy~ It's our 2 meeting

hello boy~ It's our 3 meeting

11、select语句

格式:

select 变量 in 列表
do
命令行(通常用到循环变量)
done

制作一个选择表,在列表中选择一个选项执行命令行。如果选择的变量不在列表序列中,则返回一个空值。需要用break退出循环。

例子:

#!/bin/bash

echo "a is 5 ,b is 3. Please select your method: "

a=5

b=3

select var in "a+b" "a-b" "a*b" "a/b"

do

break

done

case $var in

"a+b")

echo 'a+b= '`expr $a + $b`;;

"a-b")

echo 'a-b= '`expr $a - $b`;;

"a*b")

echo 'a*b= '`expr $a \* $b`;;

"a/b")

echo 'a/b= '`expr $a / $b`;;

*)

echo "input error"

esac

运行输出:

[fsy@localhost scripts]$ sh e.sh

a is 5 ,b is 3. Please select your method:

1) a+b

2) a-b

3) a*b

4) a/b

#? 1

a+b= 8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐