我正在尝试使用 bash 随机多次运行 python 脚本。但是,为了避免多次运行脚本,我想对其可以运行的次数设置一个上限。目前我正在使用“模数”运算符返回余数,然后在执行循环时将其用作字符串:
#!/bin/bash
RANGE=1000
number=$RANDOM
let "number %= $RANGE"
for run in {1..$number}
do
python script.py
done
随机数有效(即 $number
是 1-1000 之间的随机数),但问题是无论随机数是多少,这似乎只运行一次脚本.
可能是什么问题?
最佳答案
问题是这一行:
for run in {1..$number}
由于 {..}
范围内不允许(扩展)变量,因此无论 $ 的值是多少,您的循环都只运行一次编号
。
像这样使用它:
#!/bin/bash
range=1000
number=$((RANDOM % range))
for ((run=1; run <= number; run++)); do
python script.py
done
关于bash - 如何在bash中随机数的上限重复一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28328769/