shell脚本实例:创建不存在的文件和目录
发布时间:2014-11-24 编辑:jiaochengji.com
本文分享一段shell脚本,用来创建不存在的文件与目录,有需要的朋友参考下。
一段简单的shell脚本,用于创建不存在的文件与目录,其中创建了几个自定义函数,用来学习shell 函数很不错。
代码:
复制代码 代码示例:
#!/bin/bash
# 创建不存在的文件与目录
# 此脚本演示了shell中函数的用法,以及使用getopts命令获取命令行参数的方法
# by www.jiaochengji.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
# 创建不存在的文件与目录
# 此脚本演示了shell中函数的用法,以及使用getopts命令获取命令行参数的方法
# by www.jiaochengji.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
您可能感兴趣的文章:
python shell是什么
shell脚本实例:创建不存在的文件和目录
inux shell初级入门教程
Linux Source命令解析
深入解析tcsh的初始化配置文件
实现vi/cp/mv前自动备份源文件的shell脚本
bash shell脚本执行的几种方法
source命令执行shell文件与shell script文件名直接运行的区别
linux shell脚本编程基础教程
vps自动备份shell脚本代码
[关闭]