#!/bin/bash
# 创建不存在的文件与目录
# 此脚本演示了shell中函数的用法,以及使用getopts命令获取
命令行参数的方法
# by www.jb200.com
#
usage(){
echo "Usage: $0 {-f filename} {-d dirname}"
exit 1
}
createDir(){
if [ ! -d $1 ]
then
/bin/mkdir -p $1 >/dev/null 2>&1 && echo "Directory $1 created." || echo "Error: Failed to create $1 directory."
else
echo "Error: $1 directory exits!"
fi
}
createFile(){
if [ ! -f $1 ]
then
touch $1 > /dev/null 2>&1 && echo "File $1 created." || echo "Error: Failed to create $1 files."
else
echo "Error: $1 file exists!"
fi
}
while getopts f:d:v option
do
case "${option}"
in
f) createFile ${OPTARG};;
d) createDir ${OPTARG};;
?) usage
exit 1;;
esac
done