0

I would like to run batch script on my transmission docker. The script, should starts just after the torrent is downloaded. I receive following error:

   sh transmission-post-stop2.sh
: not foundn-post-stop2.sh: line 2: 
: not foundn-post-stop2.sh: line 13: 
: not foundn-post-stop2.sh: line 16: 
transmission-post-stop2.sh: line 18: syntax error: unexpected "("

My code is following:

#!/bin/bash

# Clears finished downloads from Transmission.

SERVER="127.0.0.1:9001 --auth xyz:xyz"
DONE_STATES=("Seeding" "Stopped" "Finished" "Idle")
if [[ -n "$TRANSMISSION_SERVER" ]]; then
    echo -n "Using server string from the environment: "
    SERVER="$TRANSMISSION_SERVER"
elif [[ "$#" -gt 0 ]]; then
    echo -n "Using server string passed through parameters: "
    SERVER="$*"
else
    echo -n "Using hardcoded server string: "
fi
echo "${SERVER: : 10}(...)"  # Truncate to not print auth.
TORRENT_LIST=$(transmission-remote $SERVER --list | sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g')
for TORRENT_ID in $TORRENT_LIST
do
    INFO=$(transmission-remote $SERVER --torrent "$TORRENT_ID" --info)
    echo -e "Processing #$TORRENT_ID: \"$(echo "$INFO" | sed -n 's/.*Name: \(.*\)/\1/p')\"..."
    # To see the full torrent info, uncomment the following line.
    # echo "$INFO"
    PROGRESS=$(echo "$INFO" | sed -n 's/.*Percent Done: \(.*\)%.*/\1/p')
    STATE=$(echo "$INFO" | sed -n 's/.*State: \(.*\)/\1/p')
 
    # If the torrent is 100% done and the state is one of the done states.
    if [[ "$PROGRESS" == "100" ]] && [[ "${DONE_STATES[@]}" =~ "$STATE" ]]; then
        echo "Torrent #$TORRENT_ID is done. Removing torrent from list."
        transmission-remote $SERVER --torrent "$TORRENT_ID" --remove
    else
        echo "Torrent #$TORRENT_ID is $PROGRESS% done with state \"$STATE\". Ignoring."
    fi
done

I'll be very grateful for any help.

Elo Bizne
  • 15
  • 5
  • 1
    The mangled error message makes me pretty sure you've got some `\r` hidden somewhere that messes things up. Check your script and data with `cat -A` or whatever tool displaying hidden chars that you're comfortable with. With `cat -A` you should be looking for `^M` in the output which corresponds to the `\r`. If you do find them, remove them with `dos2unix` or `tr -d $'\r'` – Aaron Aug 07 '21 at 10:33
  • Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Aug 07 '21 at 10:38
  • I think Aaron is right. Your script must contain `\r` because there is a (mangled) error message for line 2, which *seems* to be empty. Run `dos2unix yourScriptFile`, then try again. – Socowi Aug 07 '21 at 10:41
  • thank you for the answers - i did it. Now I receive only transmission-post-stop.sh: line 18: syntax error: unexpected "(" Line 18 is: DONE_STATES=("Seeding" "Stopped" "Finished" "Idle") – Elo Bizne Aug 07 '21 at 12:15
  • 1
    @EloBizne That error sounds like it's running under a shell that doesn't support arrays (i.e. not bash). Using `sh transmission-post-stop2.sh` overrides the shebang, and runs it with whatever `sh` is on your system (which might or might not be bash). Don't do that. Make it executable (`chmod +x transmission-post-stop2.sh`), and run it with `./transmission-post-stop2.sh`. – Gordon Davisson Aug 07 '21 at 17:41
  • 1
    As another aside -- all-caps variable names are used for variables meaningful to the shell itself. For names you define, use lowercase names -- see the relevant POSIX standard at https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that shell variables and environment variables share a namespace (as setting a shell variable will overwrite any like-named environment variable). – Charles Duffy Aug 07 '21 at 17:49

0 Answers0