2009-03-11

shell,awk,perl的分支循环结构

分支循环结构是指if,for,while,switch等的结构

1. shell
shell的分支循环结构主要特点是不用花括号定界.
1.1 if
格式是
if 测试条件 ; then
...
else
...
fi

if和then可以分写在两行上,这样就不用在它们之间加分号;如果它们写在一行上,则要加分号,而且要注意分号两边的空格.
测试条件可以用方括号'[ ]', test, '(( ))',甚至另一个分支循环结构或者命令来做. 总之,放一个布尔值进去就可以了.用[ ]测试空的字符串,值为空的变量和未初始化的变量都是false,但是数字0是true; 用(( ))的时候,则综里面的表达式的值为0时为false,不为0时为真.
例子
例子待完善

$ if [ 0 ] ; then
> echo "for note"
> fi
for note

$ if (( 0 )) ; then
> echo "for note"
> fi

if还有一个elif. 像上面例2就可以用elif来做
$ 例子待完善


1.2 for
shell的for与C语言的for有所不同,格式是
for arg in list
do
command(s)...
done

其中的in list部分是可以省略的.如果省略了它的话,则for会作用在$@上
$ func() { for i ; do echo $i ; done }

$ func 1 2 3
1
2
3

$ for i in `seq 1 10` ; do
> echo $(( $i*2 ))
> done
2
4
6
8
10
12
14
16
18
20


1.3 while和until
while和C语言的while作用一样,不过格式不同;until和while相反,只有在条件不成立的时候才执行循环
格式分别是
while [condition]
do
command...
done

until [condition-is-true]
do
command...
done

例子
$ i=0

$ while [ $i -lt 3 ] ; do echo $i; let "i++"; done
0
1
2

$ i=0

$ until [ $i -gt 3 ] ; do echo $i; let "i++"; done
0
1
2
3


1.4 break和continue
这两个和C语言的是一样的,不过这里的可以在后面加上一个数字,用来表示跳出多少重循环. break 1和continue 1和直接使用break和continue是一样的
$ while [ 0 ] ; do
> while [ 1 ] ; do
> break 2
> done
> done


1.5 switch和select
shell的switch和C语言的格式有很大不同,格式是
case "$variable" in

"$condition1" )
command...
;;

"$condition2" )
command...
;;

esac

以case开头,以esac结尾(这点和if有点相似,它以if开头,以fi结尾). 两个分号的作用相当于C语言的switch中的break
$ {
> echo "please input a number"
> read num
> case $num in
> "1")
> echo "you input number 1"
> ;;
> "2")
> echo "you input number 2"
> ;;
> esac
> }
please input a number
2
you input number 2


select也是进行多选择的. 在使用它的时候,它会提供一个菜单给用户选择.
格式是
select variable in list
do
command...
break
done

它的格式和for非常相似:它们都有一个可省略in list,在省略这个list的时候,它们都会作用在$@上. 不过select这里需要一个break来结束.
$ select i in "yes" "no"
> do
> echo "you choose $i"
> break
> done
1) yes
2) no
#? 2
you choose no


2. awk
awk的分支循环结构和C语言几乎完全一样,在这里只说明几点不同的地方
2.1 switch
awk没有switch分支语句.目前awk的switch语句还在实验阶段,使用它需要在编译的时候进行特别的配置.
2.2 for
awk的for除了和C语言一样以外,对于数组,awk的for还有相当于shell的for的功能,或者说,相当于某些语言的foreach的功能.但是awk的for的这个功能也和shell的for有些不同: 跟在awk的for后面的变量在每次循环中的值是数组的下标,而跟在shell中的for后面的变量是in后面的列表中的元素.
shell
$ for i in `seq 0 3` ; do echo $i; done
0
1
2
3

$ awk 'BEGIN{
> a[1] = 3
> a[4] = 5
> for( i in a ) {
> print a[i]
> }
> }'
5
3

另外要注意awk的for不一定按顺序把数组下标传递给i

3. perl
3.1 if
perl的if和shell的if差不多,因为perl的if也有一个怪异的elsif(shell的是elif,awk没有)
$ perl
if( 3 > 4 )
{
print "3>4"
}
elsif ( 3 > 2 )
{
print "3>2"
}
3>2


3.2 foreach
perl也有foreach,真正的foreach(因为名字叫foreach).它的格式与shell的不同之处在于,它的循环变量可省(省略的时候用$_),而后面的list不可省,而且后面的list要放在括号里
$ perl
@a = qw(1 2 3 4 5);
foreach (@a)
{
print $_
}
12345


3.3 for
当然perl也是有for循环结构的. 注意,在perl中,for和foreach可以互用,即,该写for的地方可以写成foreach;该写foreach的地方也可以写成for.也就是说,shell,awk,perl的for循环都身兼两个功能
$ cat perl.test
@a = qw /1 2 3 4 5/;
for (@a)
{
print $_;
}
for ( $i = 0; $i < 5; $i++ )
{
print $a[$i];
}
foreach( $i = 0; $i < 5; $i++ )
{
print $a[$i];
}

$ perl perl.test
123451234512345


3.4 unless
这个和if相反,只有条件为假时才执行语句
$ perl
unless( 3 > 4 )
{
print "3 <4"
}
3 <4


3.5 while
perl也有while循环,格式和C语言的是一样的
$ perl
while( $i != 3 )
{
print "hello\n";
$i++;
}
hello
hello
hello


3.6 until
perl也有像shell一样的until,它之于while,就像unless之于if一样
$ perl
until($i == 3)
{
print "for note\n";
$i++;
}
for note
for note
for note

perl和shell一样没有do...while的循环语句

perl的循环控制比C语言的要丰富
perl的last相当于C语言的break; next相当于continue; 另外,perl还有redo,它的作用是重做当前的循环.
此外,还可以在last, next, redo后面加上一个label,作用就像是shell的break N和coninue N
$ cat perl.test
while( 1 )
{
last;
}
until( $i == 3 )
{
$i++;
if( $i == 1 )
{
next;
}
print "$i\n";
}
for( $i = 0; $i < 3; $i++ )
{
print "please input a number
$input = ;
if( $input == 1 )
{
print "redo this\n";
redo;
}
print "$i\n";
}
HELLO: while(1)
{
until( 0 )
{
last HELLO;
}
}

$ perl perl.test
2
3
please input a number:1
redo this
please input a number:2
0
please input a number:3
1
please input a number:6
2


转载请注明出处 http://fornote.blogspot.com

没有评论:

发表评论