#接受一个参数,并用之创建目录,然后参数被传入命令行,重设给变量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程序将报错。