开发者

Simplest way to cycle through a bash script call

I have a bash script. I would like this script to do something different every time I call it (modulus three). Something like this :

First script call => echo "call 1"
Second script call => echo "call 2"
Third script call => echo "call 3"
Fourth script call => echo "call 1"
Fifth script call => echo "call 2"开发者_运维知识库
Sixth script call => echo "call 3"
Seventh script call => echo "call 1"
...

What would be the simplest way to do this ?

Note that the script is not critical. For example : the counter can go back to one after a reboot :

...
n script call => echo "call 1"
n+1 script call => echo "call 2"
* reboot *
n+2 script call => echo "call 1"
...

is OK.

Thank you for your insights.


Easiest approach is probably to just store the value in a file

if [[ `grep 2 config.txt` ]]
then 
    #run stuff version 2
    echo 3 > config.txt # next run will be 3
elif [[ `grep 3 config.txt` ]]
then
    #run stuff version 3
    echo 1 > config.txt # next run will be 1
else
    #run stuff version 1
    echo 2 > config.txt # next run will be 2
fi


In Bash:

call=$(< call.txt)
case $call in
    0) do_thing_one;;
    1) do_thing_two;;
    2) do_thing_three;;
    *) do_oops_thing;;
esac
echo $(( (call + 1) % 3 )) > call.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜