流程控制
if thenif (condition)
then
then-commands
fi
if then elseif (condition)
then
then-commands
else
else-commands
fi
if the elifif (condition1)
then
commands1
elif (condition2)
then
commands2
else
commands3
fi
for infor var in arg-list
do
commands
done
forfor var
do
commands
done
whilewhile (condition)
do
commands
done
untiluntil (condition)
do
commands
done
break continue casecase str in
pat1) commands1;;
pat2) commands2;;
pat3) commands3;;
esac
pat除了可以指定一些确定的字符串,也可以指定字符串的集合,如下:
* 任意字符串
? 任意字符
[abc] a、b 或 c 三字符其中之一
[a-n] 从a到n的任一字符
| 多重选择
另摘2个常用的shell实例:
将.foo后缀的文件批量改名为.bar后缀。
for f in *.foo; do
base = `basename $f .foo`
mv $f $base.bar
done
将大写文件名改为小写文件名。
for f in *; do
mv $f `echo $f | tr '[A-Z]' '[a-z]'`
done
阅读(1326) | 评论(0) | 转发(0) |