本文主要是介绍shell脚本模块化实现echo、mkdir、cp命令,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
函数实现
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
这篇关于shell脚本模块化实现echo、mkdir、cp命令的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!