您的位置:首页 > 运维架构 > Shell

Shell控制流结构编程之测试目录创建结果

2013-01-20 13:42 253 查看
#接受一个参数,并用之创建目录,然后参数被传入命令行,重设给变量DIRECTORY,最后测试变量是否为空。

#!/bin/sh

DIRECTORY=$1

if [ "$DIRECTORY" = "" ]

then

 echo "Usage:`basename $0` directory to create" >&2

 exit 1

fi

if [ -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

命令1

else

命令2

fi

记住,如果没有命令1或2,需在then或else后面加上空格+:,否则shell程序将报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: