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-FILEa 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:09findis 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/zshwhile 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:117zlike/usr/local/bin/7z. Anyone know why? When checking, Terminal is usingzshand$PATHcontains/usr/local/bin. Maybefind's-execdiris executing in a different environment? – eivindml Feb 12 '21 at 09:21/usr/local/binin$PATHfor bothbashandzsh? – Shane Bishop Feb 14 '21 at 22:13findmaybe uses bash even though the script file uses#!/bin/zsh? – eivindml Feb 16 '21 at 08:01