shell中的流控制

1. if 语句

来看 一个例子(myprog)

#!/bin/sh
if [ $# = 0 ]
then
        echo No arg
elif [ $# = 1 ]
then
        echo thereis only 1 arg
        echo it is $1
else
        echo there are more than 1 arg
        echo they are $*
fi

运行./myprog,结果为:

No arg

运行./myprog a,结果为:

thereis only 1 arg
it is a

运行./myprog a b c d e,结果为:

there are more than 1 arg
they are a b c d e

2. case语句

case string in

str1)

... ;;   #注意结束是一对;;

str2)

... ;;

.

.

.

esac

str1,str2...可以用通配符.

来看一个例子(myprog):

#!/bin/sh
read str
case str in
a)
        echo option a;;
b)
        echo option b;;
*)
        echo you inputed "$str";;
esac

运行./myprog,输入a,结果为:

you inputed a

3. for语句

for var in list

do

...

done

来看一个例子(myprog)

#!/bin/sh
for i in $(seq 1 10)
do
 echo $i
done

运行完之后结果为

1
2
3
4
5
6
7
8
9
10
4. while 及 until语句

while exp

do 

...

done


until exp

do

...

done




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值