Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4241952
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: Python/Ruby

2011-11-04 08:30:20

下面的脚本接收一个参数,并用之创建目录,然后参数被传入命令行,重设给变量DIRECORY,最后测试变量是否为空。
if [ "$DIRECTORY" = "" ] 
也可以用
if [ $# -lt 1 ]
来进行更普遍的参数测试
如果字符串为空,返回一可用信息,脚本退出。如果目录已经存在,脚本从头到尾走一遍,什么也没做。
创建前加入提示信息,如果输入Y或y,则创建目录,否则则使用空命令表示不采取任何动作。
使用最后命令状态测试创建是否成功执行,如果失败,返回相应信息。

  1. #!/bin/bash
  2. # ifmkdir.sh
  3. # parameter is passed as $1 but reassigned to DIRECTORY
  4. DIRECTORY=$1

  5. # is the string empty ??
  6. if [ "$DIRECTORY" = "" ]
  7. then
  8.     echo "Usage:'basename $0' directory to create" >&2
  9.     exit 1
  10. fi

  11. if [ -d $DIRECTORY ]
  12. then : # do nothing
  13. else
  14.     echo "the directory does not exist"
  15.     echo -n "Creatory it now?[y..n]:"
  16.     read ANS
  17.     if [ "$ANS" = "Y" ] || [ "$ANS" = "y" ]
  18.     then
  19.         echo "creating now"
  20.         #create directory and send all output to /dev/null
  21.         mkdir $DIRECTORY >/dev/null 2>&1
  22.         if [ $? != 0 ]
  23.         then
  24.             echo "errors creating the directory $DIRECTORY" >&2
  25.             exit 1
  26.         fi
  27.     else : # do noting
  28.     fi
  29. fi

  1. ywx@ywx:~/desktop/linux_shell$ ./ifmkdir.sh dtt
  2. the directory does not exist
  3. Creatory it now?[y..n]:y
  4. creating now





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