[shell] shell脚本实现将当前目录压缩成zip文件代码示例

将当前目录打包成zip压缩包代码

#!/bin/bash
current_date_time=`date +"%Y%m%d%H%M%S%3N"`

# 定义要打包的文件名或目录
src_dir="."

# 定义要排除的文件或文件目录数组
exclude_dirs=("scripts/*" ".git/*" "dist/*" ".gitignore")

# 遍历数组并将元素连接起来形成字符串
exclude_params=""
for element in "${exclude_dirs[@]}"
do
    exclude_params="$exclude_params -x \"$element\""
done

# 定义生成后zip文件名
zip_file="filename"-$current_date_time.zip

## 创建dist目录
if [ ! -d "dist" ]; then
  # 如果目录不存在,则创建目录
  mkdir "dist"
fi

# 创建zip文件
bash -c  "zip -r dist/$zip_file $src_dir $exclude_params"
echo "$zip_file created."

猜你喜欢

转载自blog.csdn.net/macaiyun0629/article/details/132154855
今日推荐