0

我编写了一个脚本,该脚本将从 Dropbox 获取 torrent 文件,将它们添加到传输并将 torrent 移动到另一个位置。该脚本对我有用,只要我一次只将一个文件放在一个文件夹中。如果文件夹中有多个文件,它会给我一个关于文件路径太长的错误。谁能从我的脚本下面告诉我我做错了什么以及如何更改它以便它可以正确处理多个文件?这是 Mac 上的一个 bash 脚本,通过 launchd 每 60 秒执行一次。

谢谢!

#!/bin/bash
# Log to debug file
set –xv; exec 1>>/Users/admin/TorrentMoverLog.txt 2>&1
date
###################################################
###                                             ###
###             Torrent Mover  v.0.1            ###
###                                             ###
###################################################

#################  Variables  #####################
###################################################
######## Watch Folders                           ##
##                                               ##
WATCH_PATH=/Users/admin/Dropbox/Torrent-Drop/
WATCH_FOLDERS=( Movies/ TV/ Music/ FLAC/ Software/ Other/ )
##                                               ## 
######## DL Folders                              ##
##                                               ##
DL_PATH=/Volumes/Media/
##                                               ##
###################################################


################  Functions  ######################
###################################################
##                                               ##
function add_torrent {
    /usr/local/bin/transmission-remote -a "$1" -w "$2""$3"
}
##                                               ##
###################################################

###################  Script  ######################
##                                               ##
## Rename files with spaces
echo "Renaming files with spaces"
find "$WATCH_PATH" -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

## Start Outer Loop
## Process Watch Folders
for wf in "${WATCH_FOLDERS[@]}"
do
    WF_LIST=`ls $WATCH_PATH${wf} | grep torrent`
    ## Check for torrent files first, skip if none found
    if [ "$WF_LIST" != "" ]; then
        echo "Processing ${wf} folder..."
        ## Start Inner Loop
        ## Process Files within watch folders
        ## Set download folder
            for torrent in "$WF_LIST";
            do
                case "${wf}" in

                Movies/ ) echo "Movies variable set"
                    DL_FOLDER=Movies/ ;;

                TV/ ) echo "TV variable set"
                    DL_FOLDER=TV_Shows/ ;;

                Music/ ) echo "Music variable set"
                    DL_FOLDER=Music/ ;;

                FLAC/ ) echo "FLAC variable set"
                    DL_FOLDER=PlexMusic/ ;;

                Software/ ) echo "Software variable set"
                    DL_FOLDER=Software/ ;;

                Other/ ) echo "Other variable set"
                    DL_FOLDER=Other/ ;;

                esac
                ## Add torrent to transmission
                echo "Running command, add_torrent $WATCH_PATH${wf}$torrent $DL_PATH $DL_FOLDER"
                add_torrent "$WATCH_PATH${wf}$torrent" "$DL_PATH" "$DL_FOLDER"
                ## Move torrent
                echo "Moving $WATCH_PATH${wf}$torrent to $DL_PATH""TorrentFiles/"
                mv "$WATCH_PATH${wf}$torrent" "$DL_PATH""TorrentFiles/"
            done
        ## End Inner Loop
        echo "Finished processing torrents in ${wf} folder."
    else
        echo "Skipping ${wf} directory, no torrents found."
    fi  
done
## End Outer Loop

exit 0
4

1 回答 1

0

你做错了什么是试图解析ls而不是使用 glob 表达式。

shopt -s nullglob # make globs return empty set on failure to match
for torrent in "$WATCH_PATH/$wf"/*torrent*; do
  echo "Found $torrent"
done

...或者,将 glob 结果存储在数组中:

shopt -s nullglob # can just put this once at the top of your script
wf_list=( "$WATCH_PATH/$wf/"*torrent* )
echo "Found ${#wf_list[@]} torrents:"
for torrent in "${wf_list[@]}"; do
  printf '- %q\n' "$torrent"
done

如果您取消引号 -- for torrent in $WF_LIST-- 这将在 的输出上启用字符串拆分和 glob 扩展ls,但这本身就是错误的(用空格破坏文件名,破坏可以解释为 glob 扩展的文件名等)。不要这样做。

于 2015-08-14T20:53:57.780 回答