How to select a random item from an array in shell

后端 未结 3 606
半阙折子戏
半阙折子戏 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:53

    Here's another solution that may be a bit more random than Jacob Mattison's solution (hard to say from the jot manpages):

    declare -a expressions=('Ploink' 'I Need Oil' 'Some Bytes are Missing' 'Poink Poink' 'Piiiip Beeeep' 'Hello' 'Whoops I am out of memory')
    index=$( jot -r 1  0 $((${#expressions[@]} - 1)) )
    selected_expression=${expressions[index]}
    

提交回复
热议问题