#!/bin/bash ShellDir() { echo $(dirname $(realpath $0)); } Main() { # local p1=$1 # local shellDir=`ShellDir` # local rules=$(cat "$shellDir/rule.txt") # echo $p1 # echo $shellDir # echo ${rules[*]} # local paths=`find $1 -depth 1` # # echo ${paths[*]} # for path in ${paths[*]} # do # echo $path # done local target="/Users/xulianxin/Documents/develop/game/TileMatch/TileStory/Resources/res_mini_story2/online" local root="/Users/xulianxin/Documents/develop/game/TileMatch/TileStory/Resources/res_mini_story2/线上剧情" rm -rf $target Recursive $1 $target $root } RelativeB2A() { # 确保传入了两个参数 if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi # 定义两个绝对路径 abs_path_1="$1" abs_path_2="$2" # 使用数组来存储分割后的路径部分 IFS='/' read -ra ADDR1 <<< "$abs_path_1" IFS='/' read -ra ADDR2 <<< "$abs_path_2" # 初始化相对路径为空 relative_path="" # 找到两个路径中共同的前缀长度 i=0 len1=${#ADDR1[@]} len2=${#ADDR2[@]} while [[ $i -lt $len1 && $i -lt $len2 && ${ADDR1[i]} == ${ADDR2[i]} ]]; do ((i++)) done j=$i # 对于ADDR2中剩余的部分,每多一个就添加一个".." while [[ ${ADDR2[i]} ]]; do relative_path="../$relative_path" ((i++)) done # 初始化一个空字符串来拼接ADDR1中剩余的部分 remaining_path="" if [ $len1 -lt $len2 ] then # 添加ADDR1中剩余的部分到相对路径 for (( ; j<$len1; j++ )); do if [[ -n "$remaining_path" ]]; then remaining_path="/$remaining_path" fi remaining_path="${remaining_path}${ADDR1[j]}" done fi if [ $len2 -lt $len1 ] then # 添加ADDR1中剩余的部分到相对路径 for (( ; j<$len1; j++ )); do if [[ -n "$remaining_path" ]]; then remaining_path="${remaining_path}/${ADDR1[j]}" else remaining_path="${remaining_path}${ADDR1[j]}" fi done fi # 如果剩余路径不为空,则将其添加到相对路径中 if [[ -n "$remaining_path" ]]; then relative_path="${relative_path}${remaining_path}" fi # 去除可能开头的多余"/" relative_path="${relative_path#/}" # 如果相对路径为空,说明两个路径相同(或者一个是另一个的子路径) if [[ -z "$relative_path" ]]; then relative_path="." fi echo $relative_path } Recursive() { local p1=$1 local p2Target=$2 local p2Root=$3 local shellDir=`ShellDir` local rules=$(cat "$shellDir/rule.txt") # echo $p1 # echo $shellDir # echo ${rules[*]} local paths=`find $1 -depth 1` # echo ${paths[*]} for path in ${paths[*]} do if [ -d $path ]; then for rule in ${rules[*]} do # echo $rule # echo $path # if [[ $path =~ ^.*/.*? ]] # then # echo $path # fi if echo "$path" | grep -oE '^.*/_.*?$' >/dev/null; then local relative=`RelativeB2A $path $p2Root` local fullpath=$p2Target/$relative local toPath=`dirname $fullpath` mkdir -p $toPath # echo $fullpath cp -RP $path $toPath fi done # echo "d "$path Recursive $path $p2Target $p2Root # else # echo "f "$path fi done } Test() { Main "/Users/xulianxin/Documents/develop/game/TileMatch/TileStory/Resources/res_mini_story2/线上剧情" } Test # Main $1