我的循环正在处理整个目录列表而不是每个单独的文件名,我做错了什么?
What am I doing wrong that my loops are processing the entire directory listing instead of each individual file name?
我写了一个脚本,可以从 Dropbox 获取 torrent 文件,将它们添加到 Transmission 并将 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 "" -w """"
}
## ##
###################################################
################### 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
你做错的是 trying to parse 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
-- 这将在 ls
的输出上启用字符串拆分和 glob 扩展,但这本身就是错误的(用空格分隔文件名,打破可以解释为 glob 扩展等的文件名)。别这样。
我写了一个脚本,可以从 Dropbox 获取 torrent 文件,将它们添加到 Transmission 并将 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 "" -w """"
}
## ##
###################################################
################### 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
你做错的是 trying to parse 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
-- 这将在 ls
的输出上启用字符串拆分和 glob 扩展,但这本身就是错误的(用空格分隔文件名,打破可以解释为 glob 扩展等的文件名)。别这样。