Chinaunix首页 | 论坛 | 博客
  • 博客访问: 662528
  • 博文数量: 137
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 1335
  • 用 户 组: 普通用户
  • 注册时间: 2005-11-23 15:18
文章分类

全部博文(137)

文章存档

2010年(2)

2009年(2)

2008年(2)

2007年(30)

2006年(99)

2005年(2)

我的朋友

分类:

2006-04-11 10:08:50

流程控制

if then
if (condition)
  then
    then-commands
fi

if then else
if (condition)
  then
    then-commands
else
    else-commands
fi

if the elif
if (condition1)
  then
    commands1
elif (condition2)
  then
    commands2
else
    commands3
fi

for in
for var in arg-list
  do
    commands
done

for
for var
  do
    commands
done

while
while (condition)
do
    commands
done

until
until (condition)
do
    commands
done

break

continue

case
case 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



阅读(1299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~