Re:Bash Retry Function with Exponential Backoff
by mmh0000 on Thursday December 29, 2022 @05:50PM
(#34179174)
If you just want to add/subtract 1, in bash you can do just '((i++))', i.e.:
Beware that the above is very -bashy- and purists will rip your head off for using it.
[ Parent
| on HN ]
Re:Bash Retry Function with Exponential Backoff
by mmh0000 on Thursday December 29, 2022 @05:53PM
(#34179209)
Also, the wooledge wiki is an amazing resource for Bash/shell scripting. To learn more magic see: https://mywiki.wooledge.org/ArithmeticExpression
Re:Bash Retry Function with Exponential Backoff
by sillysaurusx on Thursday December 29, 2022 @05:53PM
(#34179206)
Whoa. No dollar signs at all?
I’m at Lowes — does ((i+1)) work? Or is it just for incrementing / decrementing (which is still very useful!)
EDIT: thinking this over, does this work for variable substitution too? E.g. ((i)) being equivalent to $i. Not that you’d necessarily want to…
Re:Bash Retry Function with Exponential Backoff
by Arnavion on Thursday December 29, 2022 @06:04PM
(#34179321)
`(( ))` is an arithmetic evaluation block. Its content has to be an arithmetic expression. Arithmetic expressions don't require `$` before simple variable names and some more complex expressions like array indexing.
`(( i + 1 ))` will evaluate the result of adding one to `$i` and then throw away the result. It doesn't do anything useful (other than having a different exit code depending on whether the expression evaluated to 0 or not).
`$(( ... ))` evaluates the expression and then returns its value. ie `i=$(( i + 1 ))` will increment `$i`, just like `(( i++ ))` did.
Re:Bash Retry Function with Exponential Backoff
by mmh0000 on Thursday December 29, 2022 @05:55PM
(#34179220)
To add arbitrary numbers, you'd have to do an assignment, like this:
[mmh@x670]$ ((i=i+2)); echo $i
6
[mmh@x670]$ ((i=i+2)); echo $i
8
Re:Bash Retry Function with Exponential Backoff
by cmckn on Thursday December 29, 2022 @06:46PM
(#34179687)
There is a gotcha here if you use:
set -o errexit
Because *any bash arithmetic expression that evaluates to zero returns the exit code of 1*.So this works:
#!/usr/bin/env bash
set -o errexit
i=0
((++i))
echo $i # 1 will be printed to stdout
But this doesn't: #!/usr/bin/env bash
set -o errexit
i=0
((i++)) # terminates here with $? of 1
echo $i
The relevant doc is for `let`: let arg [arg ...]
Each arg is an arithmetic expression to be evaluated (see
ARITHMETIC EVALUATION above). If the last arg evaluates
to 0, let returns 1; 0 is returned otherwise.
https://www.gnu.org/software/bash/manual/bash.html#index-let
Re:Bash Retry Function with Exponential Backoff
by chubot on Thursday December 29, 2022 @08:22PM
(#34180492)
Yeah it's best to just use POSIX shell arithmetic, and not be clever, and not the other 2 ways bash has to do it! THat is:
i=$((i + 1))
The (( )) construct, without the $, is different, and it's also not POSIX. It has the gotchas around the exit code, which a plain assignment doesn't have.
Re:Bash Retry Function with Exponential Backoff
by fab23 on Thursday December 29, 2022 @06:47PM
(#34179697)
It does also matter where the plus signs are:
$ i=0
$ echo $((++i))
1
$ echo $((i++))
1
$ echo $((++i))
3
$
((++i)) does add 1 before the command is run, and ((i++)) does add 1 after the command is run.