Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Shell控制流结构编程之测试目录创建结果

#接受一个参数,并用之创建目录,然后参数被传入命令行,重设给变量DIRECTORY,最后测试变量是否为空。#!/bin/shDIRECTORY=$1
if [ "$DIRECTORY" = "" ]
then
 echo "Usage:`basename $0` directory to create" >&2
 exit 1
fiif [ -d $DIRECTORY ]
then :else
 echo "The directory not exist"
 echo -n "Create it now? [y/n]:"
 read ANS
 if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
 then
 echo "creating......"
 mkdir $DIRECTORY >/dev/null 2>&1
  if [ $? != 0 ];
  then
  echo "Errors creating the directory $DIRECTORY" >&2
  exit 1
  fi
 else :
 fi
fi这里使用最简单的嵌套的if else语句。if 条件then命令1else命令2fi记住,如果没有命令1或2,需在then或else后面加上空格+:,否则shell程序将报错。