循环 bash 脚本调用的最简单方法

发布于 2024-10-09 14:16:06 字数 586 浏览 7 评论 0原文

我有一个 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"
...

就可以了。

感谢您的见解。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2024-10-16 14:16:06

最简单的方法可能是将值存储在文件中

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

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
三寸金莲 2024-10-16 14:16:06

在重击中:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文