下面的脚本接收一个参数,并用之创建目录,然后参数被传入命令行,重设给变量DIRECORY,最后测试变量是否为空。
if [ "$DIRECTORY" = "" ]
也可以用
if [ $# -lt 1 ]
来进行更普遍的参数测试
如果字符串为空,返回一可用信息,脚本退出。如果目录已经存在,脚本从头到尾走一遍,什么也没做。
创建前加入提示信息,如果输入Y或y,则创建目录,否则则使用空命令表示不采取任何动作。
使用最后命令状态测试创建是否成功执行,如果失败,返回相应信息。
- #!/bin/bash
-
# ifmkdir.sh
-
# parameter is passed as $1 but reassigned to DIRECTORY
-
DIRECTORY=$1
-
-
# is the string empty ??
-
if [ "$DIRECTORY" = "" ]
-
then
-
echo "Usage:'basename $0' directory to create" >&2
-
exit 1
-
fi
-
-
if [ -d $DIRECTORY ]
-
then : # do nothing
-
else
-
echo "the directory does not exist"
-
echo -n "Creatory it now?[y..n]:"
-
read ANS
-
if [ "$ANS" = "Y" ] || [ "$ANS" = "y" ]
-
then
-
echo "creating now"
-
#create directory and send all output to /dev/null
-
mkdir $DIRECTORY >/dev/null 2>&1
-
if [ $? != 0 ]
-
then
-
echo "errors creating the directory $DIRECTORY" >&2
-
exit 1
-
fi
-
else : # do noting
-
fi
-
fi
- ywx@ywx:~/desktop/linux_shell$ ./ifmkdir.sh dtt
-
the directory does not exist
-
Creatory it now?[y..n]:y
-
creating now
阅读(845) | 评论(0) | 转发(0) |