random-word-pair 937 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env bash
  2. # This script will create a random word pair with an underscore ex. turtle_ladder
  3. random_word_pair() {
  4. # Constants
  5. local random_words num_random_words random_1 random_2 word_1 word_2
  6. random_words=/usr/share/dict/words
  7. # total number of non-random words available
  8. num_random_words=$(wc -l $random_words | cut -d" " -f 1)
  9. # Get two random integers
  10. random_1=$(shuf -i 1-"$num_random_words" -n 1)
  11. random_2=$(shuf -i 1-"$num_random_words" -n 1)
  12. # Get the nth word
  13. word_1=$(sed "${random_1}q;d" "$random_words")
  14. word_2=$(sed "${random_2}q;d" "$random_words")
  15. # Sanitize words
  16. word_1="${word_1,,}"
  17. word_1="${word_1//-/}"
  18. word_2="${word_2,,}"
  19. word_2="${word_2//-/}"
  20. echo "${word_1,,}_${word_2,,}"
  21. return 0
  22. }
  23. # Allow this file to be executed directly if not being sourced
  24. if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  25. random_word_pair
  26. exit $?
  27. fi