Is there a command that picks random command?

Viewed 44

I'm trying to make Jarvis like Iron Man, but the problem is how can I make Jarvis play random between two wav files. So when I say the hotword ... Jarvis should answer different between this two audio wav files. Any help?

Code:

jv_play voice/YES_1.wav || jv_play voice/YES_2.wav

Didn't work with ||

1 Answers

First place the filenames into an array:

#!/usr/bin/env bash
sounds=(
  voice/YES_1.wav
  voice/YES_2.wav
)

Then use Bash's special $RANDOM to select either one of the sounds:

# Generate a random number the size of the array
random_index=$((RANDOM%${#sounds[@]}))

jv_play "${sounds[random_index]}"
Related