我有一个bash脚本。我希望这个脚本每次调用它时都会做一些不同的事情(模数三)。像这样:
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"
...
最简单的方法是什么?
请注意,脚本并不重要。例如:重启后计数器可以返回到一个:
...
n script call => echo "call 1"
n+1 script call => echo "call 2"
* reboot *
n+2 script call => echo "call 1"
...
没问题。
感谢您的见解。
答案 0 :(得分:1)
最简单的方法可能只是将值存储在文件中
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
答案 1 :(得分:0)
在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