Skip to main content
added 361 characters in body
Source Link
its_me
  • 14.6k
  • 24
  • 58
  • 54

AboutLet me explain what the code does, I added the details in the code as comments foram trying to do by e.g. so you can understand better context,. Let's say I have 100 .torrent files in a directory. 2 of them will download xxx.epub and clarityyyy. The idea isepub respectively if added to provide it with file namea bittorrent client, but I don't know which 2 out of the 100.

So what my script does is, (s1) and it will start goinguse find to go through all the .torrent files in the current directory (and 1 level under), parsing each onepwd and checkingpass each .torrent file, as it forcomes 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(s) provided 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.

#! /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

About what the code does, I added the details in the code as comments for better context, and clarity. The idea is to provide it with file name(s) and it will start going through all the .torrent files in the current directory (and 1 level under), parsing each one and checking it for the file name(s) provided.

#! /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
done

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.

#! /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
Source Link
its_me
  • 14.6k
  • 24
  • 58
  • 54

Simple shell script unable to go through thousands of files; starts fine, but throws "unexpected EOF while looking for matching `"`" after some time

Shell Script in Question

About what the code does, I added the details in the code as comments for better context, and clarity. The idea is to provide it with file name(s) and it will start going through all the .torrent files in the current directory (and 1 level under), parsing each one and checking it for the file name(s) provided.

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
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?