重击从列表中选择随机字符串 [英] Bash select random string from list
本文介绍了重击从列表中选择随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串列表,我想在每次启动脚本时随机选择其中一个.
I have a list of strings that I want to select one of them at random each time I launch the script.
例如
SDF_BCH_CB="file1.sdf"
SDF_BCH_CW="file2.sdf"
SDF_BCH_RCB="file3.sdf"
SDF_BCH_RCW="file4.sdf"
SDF_TT="file5.sdf"
然后我想从上面的列表中随机选择以分配以下两个变量.
Then I want to be randomly select from the above list to assign the following two variables.
SDFFILE_MIN=$SDF_BCH_CW
SDFFILE_MAX=$SDF_TT
我该怎么做?
谢谢
推荐答案
存储在数组中,计数并随机选择:
Store in array, count and random select:
#!/bin/bash
array[0]="file1.sdf"
array[1]="file2.sdf"
array[2]="file3.sdf"
array[3]="file4.sdf"
size=${#array[@]}
index=$(($RANDOM % $size))
echo ${array[$index]}
这篇关于重击从列表中选择随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文