Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 837

Answer by Kamil Maciorowski for Querying transmission efficiently to tell if all torrents are finished

$
0
0

It's rather inefficient because every 10 seconds it has to probe transmission-remote twice, run awk twice, and run sort, head and find once.

Each time run transmission-remote once, use one awk to fully process the output. If I read your script right, you don't need sort nor head, nor even similar functionalities of awk. The relevant code may be like:

query_transmission | awk 'NR!=1 && $1!="Sum:" && $2!="100%" {print "busy"; exit}'

where query_transmission is a shell function (instead of $trans; see How can we run a command stored in a variable?). The snippet will print busy iff there is a non-first, non-last line that does not report 100% in the second column; otherwise the output will be empty. The last line of output from query_transmission is detected by comparing the first field to Sum:; this is not really elegant, but detecting the last line in awk in general is not elegant either.

You also don't need find. The following line will tell you the number of files in $dir (I assume the variable is set and not empty), using only the shell, i.e. without external tools:

(shopt -s dotglob nullglob; set -- "$dir"/*; echo "$#")

(a subshell, so shopt and set do not affect the main shell).

Therefore I think the condition after your while can be simplified to:

[ -n "$(query_transmission | awk 'NR!=1 && $1!="Sum:" && $2!="100%" {print "busy"; exit}')" ] \|| (shopt -s dotglob nullglob; set -- "$dir"/*; [ "$#" -gt 0 ])

In your script you should quote right.


Viewing all articles
Browse latest Browse all 837

Trending Articles