如何随机的列表,并通过随机列表迭代(庆典) [英] How to randomize a list and iterate through the randomized list (bash)

查看:143
本文介绍了如何随机的列表,并通过随机列表迭代(庆典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个小bash脚本读取命令(每行一个),在一个文本文件中。
此刻,脚本(如下所示)中,以连续的顺序(在该文件中输入的相同顺序,即)执行的命令。

I have written a little bash script that reads commands (one per line), in a text file. At the moment, the script (shown below), is executing the commands in a sequential order (i.e. in the same order entered in the file).

我想帮助修改下面的脚本,所以它读取命令到一个数组,然后遍历虽然随机列表之前随机化数组(即清单)。

I would like help to modify the script below, so that it reads the commands into an array, then randomizes that array (i.e. list) before iterating though the randomized list.

这是我迄今为止:

while read -r -a array
do
   python make_move.py "${array[@]}"
done < game_commands.dat

我在Ubuntu LTS 10.0.4

I am running bash 4.1.5 on Ubuntu 10.0.4 LTS

我需要在列表中执行所有的命令,与正在执行一次每个命令。

I need to execute ALL of the commands in the list, with each command being executed ONLY ONCE.

推荐答案

您可以使用的 SHUF 命令。

You can shuffle the lines of a file using the shuf command.

修改:使用您code SHUF 看起来

Edit: Your code using shuf would look

while read -r -a array
do
    python make_move.py "${array[@]}"
done < <(shuf game_commands.dat)

这篇关于如何随机的列表,并通过随机列表迭代(庆典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆