From a script ( running multiple times simultaneously ), I would like create a file something like:

echo "0" > /tmp/count

Then I would like to do three functions.

incr /tmp/count
decr /tmp/count
is_zero /tmp/count

which do thje fairly obvious thing: increment the value in /tmp/count, decrement the value in /tmp/count and test if the value is_zero.

I can write three C programs to do this, but am hoping that there already exists a way. Any suggestions?

incr() {
    var=$(< "$1")
    var=$((var + 1))
    echo $var > "$1"

is_zero() {
    var=$(< "$1")
    [[ var -eq 0 ]]

decr is left as an exercise for the reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.