You know why I love my blog? It’s because of everybody who reads it. Thanks y’all.
Case in point: my fix to randomize shell script execution went from
# Don't slam the mail servers!
sleepamt=$(echo "$RANDOM/1092" | bc)
/bin/sleep $sleepamt
to
# Don't slam the mail servers!
/bin/sleep $(($RANDOM/1092))
Hehe. It’s like extreme programming, writ large. The second case has fewer dependencies, though, and I like that.
Expressions with double brackets [[ and double parens (( are bash-isms.
# echo $0; /bin/sleep $(($RANDOM/1092))
sh
sh: arithmetic expression: expecting primary: “/1092”
If bashisms are truly required the script should be called with #!/bin/bash explicitly, and not implicitly with #!/bin/sh. Conflating Bourne Again Shell with Bourne was one of the biggest crimes originally committed by Linux distributions, and one only recently being corrected by some. Debian and Ubuntu now use dash, an ash derivative Bourne implementation, as their #!/bin/sh.
I believe expr would have the edge over bc for portability; bc isn’t a core installation component of Linux but expr is, if I’m not mistaken. Unfortunately for the portability of this implementation, $RANDOM is a feature of bash as well.
No, I was wrong. Only tests with double brackets are bash-isms. $RANDOM is still a bash feature, but double-parens can be used portably.