2

我正在尝试编写一个脚本,该脚本将抓取我的 Plex 媒体文件夹,找到任何标题“.r00”文件,将它们解压缩到它们自己的目录中,并在完成后丢弃存档 zip。我有两个我一直在玩的选择。结合他们做我想做的事,但我想把这一切都放在一个漂亮的小脚本中。

选项1:

  • 此脚本打开“LinRAR”GUI,让我导航到特定目录,在该目录中查找并提取任何 .r00 文件,并成功删除所有存档 zip。

    while true; do
    if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
        if [[ ! -d $dir ]]; then
    echo "$dir: Wrong Directory" >&2
    else
    ( cd "$dir" && for f in *.r00; do [[ -f $f ]] || continue; rar e "$f" && rm "${f%00}"[0-9][0-9]; done )
    fi
    else
    echo "$bold Selection cancelled $bold_off" >&2
        exit 1
    fi
    zenity --title="What else...?" --question --text="More work to be done?" || break
    done
    

选项 2:

  • 此脚本 cd 到我的 Plex 文件夹,递归查找任何 .r00 文件,解压缩到我的 /home/user 文件夹,并且不删除存档 zip。

    (cd '/home/user/Plex');
     while [ "`find . -type f -name '*.r00' | wc -l`" -gt 0 ]; 
          do find -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;; 
    done
    

我希望有一些东西可以使用第一个工作脚本,并将递归查找应用于 /Plex 内的所有文件夹,而不是让我一次通过“LinRAR”GUI 导航到一个文件夹。

4

1 回答 1

0

无需使用cd. find需要一个起始目录。就是您传递给
它的那个点 ( )。.

还为查找和循环添加了另一个(更理智的)替代方案:

#!/bin/bash

while true; do
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then 
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong Directory" >&2
    else
        # Alternative 1 - a little more comfortable
        files="$(find "${dir}" -type f -name '*.r00')"
        for file in ${files}; do
            rar e "${file}" && rm "${file}"
        done

        # Alternative 2 - based on your original code
        while [ "`find "${dir}" -type f -name '*.r00' | wc -l`" -gt 0 ]; do
            find "${dir}" -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;;
        done
    fi
else
    echo "$bold Selection cancelled $bold_off" >&2
    exit 1
fi
zenity --title="What else...?" --question --text="More work to be done?" || break
done

根据评论,我运行了这个代码的一个小例子,它工作得很好:

#!/bin/bash

if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
    if [[ ! -d $dir ]]; then
        echo "$dir: Wrong directory" >&2
    else
        find $dir -type f
    fi
else
    echo "cancelled"
fi

成功选择了一个目录并打印了它的所有文件。如果我选择取消,那么它会打印“已取消”。

于 2015-04-19T15:40:23.277 回答