Shell Script in Question
Let me explain what I am trying to do by e.g. so you can understand better. Let's say I have 100 .torrent files in a directory. 2 of them will download xxx.epub and yyy.epub respectively if added to a bittorrent client, but I don't know which 2 out of the 100.
So what my script does is, (1) use find
to go through all .torrent files in pwd
and pass each .torrent file, as it comes by, to transmission-show
which will parse the .torrent file and output metadata in human readable format. We'll then use awk
to get the file name the torrent file will download and run that against the list.txt which has file names we are looking for, i.e. xxx.epub and yyy.epub.
File: findtor-array.sh
#! /bin/bash
#
# Search .torrent file based on 'Name' field.
#
# USAGE:
# cd ~/myspace # location of .torrent files
# Run `findtor ~/list.txt` (if `findtor.sh` is placed in `~/bin` or `~/.local/bin`)
# Turn the list of file names from ~/list.txt (or any file passed as argument) into an array
readarray -t FILE_NAMES_TO_SEARCH < "$1"
# For each file name from the list...
for FILE_NAME in "${FILE_NAMES_TO_SEARCH[@]}"
do
# In `pwd` and 1 directory-level under, look for .torrent files and search them for the file name
find . -maxdepth 2 -name '*.torrent' -type f -exec bash -c "transmission-show \"\$1\" | awk '/^Name\: / || /^File\: /' | awk -F ': ' '\$2 ~ \"$FILE_NAME\" {getline; print}'" _ {} \; >> ~/torrents.txt
# The `transmission-show` command included in `find`, on it own, for clarity:
# transmission-show xxx.torrent | awk '/^Name: / || /^File: /' | awk -F ': ' '$2 ~ "SEARCH STRING" {getline; print}'
done
I think the process is simple and I am doing it right (except there are no checks, I know). But somehow the whole task seems too much for the script, because after running it, after sometime it starts throwing these errors continuously until I Ctrl + C it:
_: -c: line 0: unexpected EOF while looking for matching `"'
_: -c: line 1: syntax error: unexpected end of file
Are these "scaling" issues? What am I missing and what can I do to fix it?
"
? Are they regular expressions? You are currently injecting the contents of the file as code into yourawk
program. It would be better to pass the values properly, as described in https://unix.stackexchange.com/questions/120788/pass-shell-variable-as-a-pattern-to-awk (I would additionally switch the order of thefind
and thefor
loop so thatfind
calls a script that loops, rather than the other way around). – Kusalananda Apr 04 '20 at 07:34awk
code and will end the current single-quoted string where it occurs. Consider passing the string properly withawk -v
instead. – Kusalananda Apr 04 '20 at 09:56