一.createDir.sh的编写,如下:
#! /bin/bash
isDirectory(){DIR_NAME=$1
if [ ! -d $DIR_NAME ];then
return 1
else
return 0
fi
}
for DIR in "$@";do
if isDirectory "$DIR"
then :
else
echo "$DIR does not exist.Creating it now..."
mkdir $DIR > directory.log 2>&1
if [ $? -ne 0 ];then
echo "Cannot create directory $DIR"
exit 1
fi
fi
done
说明:(1)这个脚本可以一次创建多个目录,通过命令行参数传入各目录名,脚本先逐个测试各目录是否存在,如果目录不存在,首先打印信息然后试着创建该目录。
(2)该脚本包含了一些shell最基本的语法,如下:
a)控制语句:if/else/fi
b)测试语句:[
c)循环语句:for/do/done
d)函数使用:isDirectory
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8248926