You are not logged in.
This is all about one variable ($progress) which, sadly, will rarely equal zero, resulting in an annoying "division by zero!" error.
expectedCompletion="$( echo 3k $elapsedTime 10000 \* $progress / $totalTime / 0k 0.5 + 1 / f | dc )"
The only "fix" I've been able to think about so far, is to add one to $progress when it's equal to zero.
However, as I am a bash noob, I am wondering if there's a better solution. If for example I could catch the error, not output an error massage, but instead return a value of my choice, that's be cool.
There may also be a mathematical solution to the problem, but I certainly don't see it.
In any case, thanks in advance.
Last edited by zacariaz (2012-09-11 21:08:24)
I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.
Offline
Don't do the calculation if progress is zero?
expectedCompletion="$( [ $progress -eq 0 ] && echo "progress is 0" || echo 3k $elapsedTime 10000 \* $progress / $totalTime / 0k 0.5 + 1 / f | dc )"
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Don't do the calculation if progress is zero?
expectedCompletion="$( [ $progress -eq 0 ] && echo "progress is 0" || echo 3k $elapsedTime 10000 \* $progress / $totalTime / 0k 0.5 + 1 / f | dc )"
But how to achieve that? Won't it still be evaluated, even if surrounded by an if statement or similar?
I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.
Offline
What I added is something like an if statement, and works as expected. It relies on some implementation details like that the expression is grouped and evaluated from left to right and that the execution is aborted as soon as the endresult can be determined.
Edit: It also relies on echo never failing. Otherwise you could end up with some unexpected results like "progress 0, echo fail, so calculate result"
var=$( [ cond -eq 0 ] && echo a || echo b )
-> set var to the stdout of the shell command:
test if cond equals 0
&&: If true (exitcode 0)
echo a
||: test and echo both with exitcode 0, so the or is already true, do nothing
&&: False (exitcode not 0), so skip echo a
||: we arent't successful yet, so
echo b
If you want to use a more straight forward if construct, it would be something like this:
if [ $progress -eq 0]; then
expectedCompletion="progress is 0"
else
expectedCompletion="$( ... YOURCOMMAND ...)"
fi
Last edited by progandy (2012-09-11 21:16:29)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
What I added is something like an if statement, and works as expected. It relies on some implementation details like that the expression is grouped and evaluated from left to right and that the execution is aborted as soon as the endresult can be determined.
If you want to use a more straight forward if construct, it would be something like this:if [ $progress -eq 0]; then expectedCompletion="progress is 0" else expectedCompletion="$( ... YOURCOMMAND ...)" fi
Mkay, I basically didn't think it worke that way, though of course it should.
Bash is very confusing.
Thanks a bunch.
I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.
Offline
Could also do:
expectedCompletion="$( ((progress == 0)) && echo 0 || YOURCOMMAND )"
Offline