函数实现
echo命令模块化:
#红色log显示
function fail_info()
{
echo -e "\033[31m $1 \033[0m"
exit 1
}
#绿色log显示
function success_info()
{
echo -e "\033[32m $1 \033[0m"
}
#黄色log显示
function debug_info()
{
echo -e "\033[33m $1 \033[0m"
}
mkdir命令模块化:
#创建文件夹,存在则删除再创建
function create_folder()
{
dir=$1
if [ -d "$dir" ];then
debug_info "folder $dir is exists, delete it"
rm -rfv "$dir"
success_info "recreate folder $dir"
mkdir -p "$dir"
else
mkdir -p "$dir"
success_info "create folder $dir success"
fi
}
cp命令模块化
#可拷贝文件/文件夹至指定目录,失败则正常退出
function copy_to_dir()
{
src=$1
dir=$2
debug_info "src=[$src] dir=[$dir]"
if [ ! -d "$dir" ];then
fail_info "directory $dir is not exist"
fi
if [ -f "$src" ];then
cp -rfv "$src" "$dir"
success_info "copy folder $dir success"
elif [ -d "$src" ];then
cp -rfv "$src" "$dir"
success_info "copy file $dir success"
else
fail_info "file or folder $src is not exist"
fi
}
函数用例
cd /usr/lib
create_folder new_folder
copy_to_dir new_folder /usr/bin