Score:0

Issue with if statment in bash

rw flag

First time interacting here. it helped me several times, but I'm lost this time. I got a script to remove stalled/ended torrents from @bulljit. (I don't know if I can/should put his Github here. Sorry if I'm wrong). The original code is here: https://gist.github.com/bulljit/791609/3d3e3f9ed7d3d9ad140c2bade503b5864bd475bb

But I decided to go one step further and set a RATIO value for the seeding since my internet connection is not the best around. Here is what I came up with: (Thechanges that I made is inside the {} since there is none of it in the code.)

#!/bin/sh
# the folder to move completed downloads to port, username,
# password
SERVER="192.168.1.105:9091"

# use transmission-remote to get torrent list from
# transmission-remote list use sed to delete first / last line
# of output, and remove leading spaces use cut to get first
# field from each line
TORRENTLIST=`transmission-remote $SERVER --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=" " --fields=1`
transmission-remote $SERVER --list

# for each torrent in the list
for TORRENTID in $TORRENTLIST
do
echo Processing : $TORRENTID

# check if torrent download is completed
DL_COMPLETED=`transmission-remote $SERVER --torrent $TORRENTID --info | grep "Percent Done: 100%"`

# check torrents current state is
STATE_STOPPED=`transmission-remote $SERVER --torrent $TORRENTID --info | grep "State: Seeding\|Stopped\|Finished\|Idle"`
echo $STATE_STOPPED
{
**#check if the selected Ratio was completed
RATIO=`echo "$INFO" | grep "Ratio: 0.5"`**
}
# if the torrent is "Stopped", "Finished", or "Idle" after
# downloading 100%"
if [ "$DL_COMPLETED" ] && [ "$STATE_STOPPED" ] {&& [ "$RATIO" != "0.5" ]}; then

echo "$SERVER and $TORRENTID and TORRENTLIST"

# move the files and remove the torrent from Transmission
echo "Torrent #$TORRENTID is completed"
echo "Removing torrent from list"
transmission-remote $SERVER --torrent $TORRENTID --remove
else
echo "Torrent #$TORRENTID is not completed. Ignoring."
fi
done

The issue is that I don't download as much as one can think I do, but I would like to mess around and play with such features. But all torrents are removed, not only the ones that finished seeding. Sorry if I'm in the wrong forum...

pl flag
It's not bash if you're using `#!/bin/sh`
Tsimoes avatar
rw flag
You're right. But I don't think that change to #!/bin/bash would solve my issue. Or would?
hr flag
If you grep for `Ratio: 0.5` then the result will never be *equal to* `0.5` - you probably just want to use the grep *exit status* i.e. `if ... && echo "$INFO" | grep -q 'Ratio: 0.5'; then` or (if you actually use bash) `if ... && grep -q 'Ratio: 0.5' <<<"$INFO"; then`
hr flag
... I'd also suggest (1) properly indenting the code; (2) replacing ALLCAPS variable names; (3) changing deprecated "backticks" for the command substitutions to modern `$(...)` and if possible (4) re-writing the for loop so it doesn't rely on word-splitting of the unquoted variable `$TORRENTLIST`
waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process.
Tsimoes avatar
rw flag
@steeldriver - I'll have to learn a lot more to do all your recommendations. Thanks for pointing that direction. waltinator - Never heard of that before. Already took a look to see how to use it to improve my scripts. Thanks a lot guys.
hr flag
@Tsimoes it's a learning process for us all - it's just unfortunate that the script you are starting from demonstrates some out-of-date and/or non-recommended practices
Cyrus avatar
cn flag
Fix the bugs in your shown script.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.