假设这样的脚本
#!/bin/bash
array[0]="123"
array[1]="333"
array[2]="5566"
array[3]="135"
array[4]="68"
array[5]="45"
array[6]="78"
array[7]="32"
array[8]="190"
number()
{
size=${#array[@]}
index=$(($RANDOM % $size))
sleep 0.5s
echo Lucky number is ... ${array[$index]}
}
for i in {1..21}; do number; done
没关系..我想在间隔中获得随机数,但也显示很多重复
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 190
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 45
Lucky number is ... 68
Lucky number is ... 68
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 78
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 333
Lucky number is ... 135
我想要这样的结果
Lucky number is ... 135
Lucky number is ... 5566
Lucky number is ... 78
Lucky number is ... 190
Lucky number is ... 333
Lucky number is ... 45
Lucky number is ... 68
这样我们就可以不用重复获得“幸运号码”了,唯一的 有人知道怎么做吗?编辑脚本并将 21 减少到 9 不会改变,我什至得到重复。
Lucky number is ... 135
Lucky number is ... 68
Lucky number is ... 45
Lucky number is ... 333
Lucky number is ... 78
Lucky number is ... 135
Lucky number is ... 135
Lucky number is ... 333
Lucky number is ... 32
将列表视为一副纸牌。 随机播放数字,然后一次读出一个。
请参阅在 BASH shell 中打乱数组元素的简单方法?.