[转] 6. Bourne Shell及shell编程
4耸比绻?
访问
前9个参数之后的参数,就必须使用shift.
另外shift后可加整数进行一次多个移位,如:
shift 3
<4>. for循环
格式:
for var in arg1 arg2 ... argn
do
command
....
command
done
示例:
$ for letter in a b c d e; do echo $letter;done
a
b
c
d
e
对当前目录下的所有文件操作:
$ for i in *
do
if [ -f $i ]
then
echo "$i is a file"
elif [ -d $i ]
echo "$i is a directory"
fi
done
求命令行上所有整数之和:
#!/bin/sh
sum=0
for INT in $*
do
sum=`expr $sum + $INT`
done
echo $sum
<6> 从循环中退出: break和continue命令
break 立即退出循环
continue 忽略本循环中的其他命令,继续下一下循环
在shell编程中有时我们要用到进行无限循环的技巧,也就是说这种循环一直执行
碰
到break或continue命令。这种无限循环通常是使用true或false命令开始的。UNIX
系统中的true总是返加0值,而false则返回非零值。如下所示:
#一直执行到程序执行了break或用户强行中断时才结束循环
while true
do
command
....
command
done
上面所示的循环也可以使用until false, 如下:
until false
do
command
....
command
done
在如下shell script中同时使用了continue,break以及case语句中的正规表达式用
法:
#!/bin/sh
# Interactive program to restore, backup, or unload
# a directory
echo "Welcome to the menu driven Archive program"
while true
do
# Display a Menu
echo
echo "Make a Choice from the Menu below"
echo _
echo "1 Restore Archive"
echo "2 Backup directory"
echo "3 Unload directory"
echo "4 Quit"
echo
# Read the user's selection
echo -n "Enter Choice: "
read CHOICE
case $CHOICE in
[1-3] ) echo
# Read and validate the name of the directory
echo -n "What directory do you want? "
read WORKDIR
if [ ! -d "$WORKDIR" ]
then
echo "Sorry, $WORKDIR is not a directory"
continue
fi
# Make the directory the current working directory
cd $WORKDIR;;
4) :;; # :为空语句,不执行任何动作
*) echo "Sorry, $CHOICE is not a valid choice"
continue
esac
case "$CHOICE" in
1) echo "Restoring...还有问题请来论坛寻求帮助:
访问
前9个参数之后的参数,就必须使用shift.
另外shift后可加整数进行一次多个移位,如:
shift 3
<4>. for循环
格式:
for var in arg1 arg2 ... argn
do
command
....
command
done
示例:
$ for letter in a b c d e; do echo $letter;done
a
b
c
d
e
对当前目录下的所有文件操作:
$ for i in *
do
if [ -f $i ]
then
echo "$i is a file"
elif [ -d $i ]
echo "$i is a directory"
fi
done
求命令行上所有整数之和:
#!/bin/sh
sum=0
for INT in $*
do
sum=`expr $sum + $INT`
done
echo $sum
<6> 从循环中退出: break和continue命令
break 立即退出循环
continue 忽略本循环中的其他命令,继续下一下循环
在shell编程中有时我们要用到进行无限循环的技巧,也就是说这种循环一直执行
碰
到break或continue命令。这种无限循环通常是使用true或false命令开始的。UNIX
系统中的true总是返加0值,而false则返回非零值。如下所示:
#一直执行到程序执行了break或用户强行中断时才结束循环
while true
do
command
....
command
done
上面所示的循环也可以使用until false, 如下:
until false
do
command
....
command
done
在如下shell script中同时使用了continue,break以及case语句中的正规表达式用
法:
#!/bin/sh
# Interactive program to restore, backup, or unload
# a directory
echo "Welcome to the menu driven Archive program"
while true
do
# Display a Menu
echo
echo "Make a Choice from the Menu below"
echo _
echo "1 Restore Archive"
echo "2 Backup directory"
echo "3 Unload directory"
echo "4 Quit"
echo
# Read the user's selection
echo -n "Enter Choice: "
read CHOICE
case $CHOICE in
[1-3] ) echo
# Read and validate the name of the directory
echo -n "What directory do you want? "
read WORKDIR
if [ ! -d "$WORKDIR" ]
then
echo "Sorry, $WORKDIR is not a directory"
continue
fi
# Make the directory the current working directory
cd $WORKDIR;;
4) :;; # :为空语句,不执行任何动作
*) echo "Sorry, $CHOICE is not a valid choice"
continue
esac
case "$CHOICE" in
1) echo "Restoring...还有问题请来论坛寻求帮助:
浙公网安备 33010602011771号