mirror of
https://gitee.com/amber-ce/amber-ce-bookworm.git
synced 2025-12-14 02:52:04 +08:00
update: 通用ACE
This commit is contained in:
62
replacer.sh
Normal file
62
replacer.sh
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 读取配置文件并解析键值对
|
||||
declare -A REPLACEMENTS
|
||||
while IFS='=' read -r key value; do
|
||||
[[ $key =~ ^@.*@$ ]] || continue # 仅处理形如 @VAR@ 的键
|
||||
REPLACEMENTS["$key"]="$value"
|
||||
done < ace-base.config
|
||||
|
||||
# 目标目录(默认当前目录)
|
||||
if [[ -z "$1" ]];then
|
||||
echo "Need appoint target dir."
|
||||
exit
|
||||
fi
|
||||
|
||||
|
||||
TARGET_DIR="${1}"
|
||||
|
||||
# 替换文件内容
|
||||
replace_in_files() {
|
||||
local file="$1"
|
||||
local temp_file="${file}.tmp"
|
||||
|
||||
# 遍历所有替换规则
|
||||
cp "$file" "$temp_file"
|
||||
for key in "${!REPLACEMENTS[@]}"; do
|
||||
sed -i "s|$key|${REPLACEMENTS[$key]}|g" "$temp_file"
|
||||
done
|
||||
|
||||
# 确保有更改后才覆盖原文件
|
||||
if ! cmp -s "$file" "$temp_file"; then
|
||||
mv "$temp_file" "$file"
|
||||
echo "Updated content in: $file"
|
||||
else
|
||||
rm "$temp_file"
|
||||
fi
|
||||
}
|
||||
|
||||
# 递归扫描目录,处理文本文件的内容
|
||||
find "$TARGET_DIR" -type f | while read -r file; do
|
||||
if file --mime-type "$file" | grep -q 'text/'; then
|
||||
replace_in_files "$file"
|
||||
fi
|
||||
done
|
||||
|
||||
# 处理文件名和目录名
|
||||
find "$TARGET_DIR" -depth | while read -r old_name; do
|
||||
new_name="$old_name"
|
||||
|
||||
# 替换文件名中的占位符
|
||||
for key in "${!REPLACEMENTS[@]}"; do
|
||||
new_name="${new_name//$key/${REPLACEMENTS[$key]}}"
|
||||
done
|
||||
|
||||
# 如果名称有变化,则重命名
|
||||
if [[ "$old_name" != "$new_name" ]]; then
|
||||
mv "$old_name" "$new_name"
|
||||
echo "Renamed: $old_name -> $new_name"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Processing completed."
|
||||
Reference in New Issue
Block a user