0

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?

eivindml
  • 111
  • Is /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:09
  • The nested find 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
  • It's a folder, sorry about the confusion! Thanks for the suggestion, I will try that when I'm in front of my office server. :) I think 7z have to be executed in the same dir, som that is a good suggestion. :) – eivindml Feb 10 '21 at 17:16
  • Tested now, it had the same problems. Worked directly in the Terminal, but didn't work from the transmission script. But I figured it out, the issue was that Terminal is using /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:11
  • Your's work if I specify absolute path to 7z like /usr/local/bin/7z. Anyone know why? When checking, Terminal is using zsh and $PATH contains /usr/local/bin. Maybe find's -execdir is executing in a different environment? – eivindml Feb 12 '21 at 09:21
  • @eivindml Is /usr/local/bin in $PATH for both bash and zsh? – Shane Bishop Feb 14 '21 at 22:13
  • No, I probably haven't added that to Bash (not in front of that computer). So find maybe uses bash even though the script file uses #!/bin/zsh? – eivindml Feb 16 '21 at 08:01

0 Answers0