I'm trying to create a bash script that is executed by Transmission when a torrent file is downloaded, and automatically extract *.rar
files into the same directory.
This is what I have
#!/bin/bash
echo "------" >> /tmp/transmission.log
date >> /tmp/transmission.log
echo "$TR_TORRENT_DIR" >> /tmp/transmission.log
echo "$TR_TORRENT_NAME" >> /tmp/transmission.log
echo "Trying to extract: $TR_TORRENT_DIR/$TR_TORRENT_NAME" >> /tmp/transmission.log
find "$TR_TORRENT_DIR/$TR_TORRENT_NAME" -type d -maxdepth 1 -exec /usr/local/bin/bash -c 'cd "{}" && find . -name "*.rar" | xargs /usr/local/bin/7z x' \; &>> /tmp/transmission.log
The script is triggered, and everything looks fine, but there is no log output from the last line of the script, and the rar is never extracted. The script has chmod +x
.
/tmp/transmission.log
shows
------
Wed Feb 10 12:23:40 CET 2021
/Volumes/TV
My.File.720p.HDTV.x264-FILE
Trying to extract: /Volumes/TV/My.File.720p.HDTV.x264-FILE
What I've tried
If I manually set export TR_TORRENT_NAME=...
and export TR_TORRENT_DIR=...
in Terminal and run the command from the last line of the script, then the file is actually extracted properly.
What could be going on here? How can I debug this issue further?
{}
in the shell code! – Kamil Maciorowski Feb 10 '21 at 11:42/Volumes/TV/My.File.720p.HDTV.x264-FILE
a directory or a file? The name indicates a file; if so,find /Volumes/TV/My.File.720p.HDTV.x264-FILE -type d ...
doesn't find anything, therefore no output is generated. – berndbausch Feb 10 '21 at 14:09find
is overly complex.find "$TR_TORRENT_DIR/$TR_TORRENT_NAME" -maxdepth 1 -name "*.rar" -execdir 7z {} \;
(not sure if this is correct) executes the 7z command in the directory where the rar file resides. – berndbausch Feb 10 '21 at 14:16/bin/zsh
while the script was using/bin/bash
. But your simpler solution still didn't work properly from the script, so I have to use this complex line:find "$TR_TORRENT_DIR/$TR_TORRENT_NAME" -type d -maxdepth 1 -exec /bin/zsh -c 'cd "{}" && find . -name "*.rar" | xargs /usr/local/bin/7z x' \; &>> /Users/user/Library/Logs/transmission.log
. If you know what could be wrong, I would love to use the simple version. – eivindml Feb 12 '21 at 09:117z
like/usr/local/bin/7z
. Anyone know why? When checking, Terminal is usingzsh
and$PATH
contains/usr/local/bin
. Maybefind
's-execdir
is executing in a different environment? – eivindml Feb 12 '21 at 09:21/usr/local/bin
in$PATH
for bothbash
andzsh
? – Shane Bishop Feb 14 '21 at 22:13find
maybe uses bash even though the script file uses#!/bin/zsh
? – eivindml Feb 16 '21 at 08:01