How to select a random item from an array in shell

后端 未结 3 619
半阙折子戏
半阙折子戏 2020-12-01 03:54

I\'m creating a bot in Shell Script:

# Array with expressions
expressions=(\"Ploink Poink\" \"I Need Oil\" \"Some Bytes are Missing!\" \"Poink Poink\" \"Piii         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 04:46

    Change the line where you define selectedexpression to

    selectedexpression=${expressions[$RANDOM % ${#expressions[@]} ]}

    You want your index into expression to be a random number from 0 to the length of the expression array. This will do that.

提交回复
热议问题