I'd have a couple of possible (say string) values: a b c. I'd like a to switch from one to another in a circular way:
if [ "$value" == "a" ]; then value=b
elif [ "$value" == "b"]; then value=c
elif [ "$value" == "c"]; then value=a
fi
With method like this, Can this be done in a more efficient way, so that I could support many options, like a b c ... z?
It can be done like this:
get_next()
{
__current_i=${__current_i:- 0} # global variable that saves index of current value
[ $__current_i -ge $# ] && __current_i=0 # if we exceed the number of values, then reset to zero
shift $__current_i # move to current value
((__current_i++)) # increment index
echo $1
}
val="a b c d"
get_next $val
get_next $val
get_next $val
get_next $val
get_next $val
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With