本文介绍了diff两个数组,每个数组包含文件路径到第三个数组(用于删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的功能中,您会看到有关解决该问题的几种尝试的注释;每次尝试都有一条注释,指出出了什么问题.在我的尝试之间,有一个来自此处的问题.解决问题的某些要素.同样,我添加了一条注释,指出应该解决的问题.我的大脑在这一点上很糊涂.我忽略的愚蠢简单事情是什么?

In the function below you will see notes on several attempts to solve this problem; each attempt has a note indicating what went wrong. Between my attempts there is a line from another question here which purports to solve some element of the matter. Again, I've added a note indicating what that is supposed to solve. My brain is mush at this point. What is the stupid simple thing I've overlooking?

function func_removeDestinationOrphans() { 
    readarray -d '' A_Destination_orphans < <( find "${directory_PMPRoot_destination}" -type f -print0 ) 
    for (( i = 0 ; i < ${#A_Destination_orphans[@]} ; i++ )) ; do 
        printf '%s\n' "→    ${A_Destination_orphans[${i}]}" # path to each track 
    done 
    printf '%b\n' "" 
    # https://stackoverflow.com/questions/2312762/compare-difference-of-two-arrays-in-bash 
    # echo ${Array1[@]} ${Array2[@]} | tr ' ' '\n' | sort | uniq -u ## original 
    # Array3=(`echo ${Array1[@]} ${Array2[@]} | tr ' ' '\n' | sort | uniq -u `) ## store in array 
    # A_Destination_orphans_diff=(`echo "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | tr ' ' '\n' | sort | uniq -u `) # drops file path after space 
    # printf "%s\0" "${Array1[@]}" "${Array2[@]}" | sort -z | uniq -zu ## newlines and white spaces 
    # A_Destination_orphans_diff=($( printf "%s\0" "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | sort -z | uniq -zu )) # throws warning and breaks at space but not newline 
    # printf '%s\n' "${Array1[@]}" "${Array2[@]}" | sort | uniq -u ## manage spaces 
    # A_Destination_orphans_diff=($( printf '%s\n' "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | sort | uniq -u )) # breaks at space and newline 
    # A_Destination_orphans_diff="($( printf '%s\n' "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | sort | uniq -u ))" # creates string surrounded by () 
    # A_Destination_orphans_diff=("$( printf '%s\n' "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | sort | uniq -u )") # creates string 
    # A_Destination_orphans_diff=($( printf '%s\n' ${A_Destination_dubUnders[@]} ${A_Destination_orphans[@]} | sort | uniq -u )) # drops file path after space 
    for (( i = 0 ; i < ${#A_Destination_orphans_diff[@]} ; i++ )) ; do 
        printf '%s\n' "→    ${A_Destination_orphans_diff[${i}]}" # path to each track 
    done 
    printf '%b\n' "" 
    for (( i = 0 ; i < ${#A_Destination_orphans_diff[@]} ; i++ )) ; do 
        echo # rm "${A_Destination_orphans_diff[i]}" 
    done 
    func_EnterToContinue 
} 

推荐答案

这里是经过修改的工作版本,这要归功于前两个受访者的建议输入(谢谢!).

Here is the working version with modifications thanks to suggested input from the first two respondents (thanks!).

function func_removeDestinationOrphans() { 
    printf '%s\n' " →   Purge playlist orphans:  " "" 
    printf '%b\n' "First we will remove any files not present in your proposed playlist.  " 
    func_EnterToContinue 
    bash_version="$( bash --version | head -n1 | cut -d " " -f4 | cut -d "(" -f1 )" 
    if printf '%s\n' "4.4.0" "${bash_version}" | sort -V -C ; then 
        readarray -d '' A_Destination_orphans < <( find "${directory_PMPRoot_destination}" -type f -print0 ) # readarray or mapfile -d fails before bash 4.4.0 
        readarray -t -d '' A_Destination_orphans_diff < <( 
            printf "%s\0" "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | 
            sort -z | 
            uniq -zu 
        ) 
    else 
        while IFS=  read -r -d $'\0'; do 
            A_Destination_orphans+=( "$REPLY" ) 
        done < <( find "${directory_PMPRoot_destination}" -type f -print0 ) 
        IFS=$'\37' read -r -d '' -a A_Destination_orphans_diff < <( 
        printf "%s\0" "${A_Destination_dubUnders[@]}" "${A_Destination_dubUnders[@]}" "${A_Destination_orphans[@]}" | 
            sort -z | 
            uniq -zu | 
            xargs -0 printf '%s\37' 
        ) 
    fi 
    if [[ ! "${A_Destination_orphans_diff[*]}" = '' ]] ; then 
        for (( i = 0 ; i < ${#A_Destination_orphans_diff[@]} ; i++ )) ; do 
            rm "${A_Destination_orphans_diff[i]}" 
        done 
    fi 
} 

如果您想查看整个Personal Music Player同步脚本,可以通过我的 GitHub .

If you would like to see the entire Personal Music Player sync script, you can find that via my GitHub.

这篇关于diff两个数组,每个数组包含文件路径到第三个数组(用于删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 08:08