Random

My problem is as follow and i hope you can help:

I currently have this function:


stored_word()
{
number=$RANDOM
let "number %= 21"
case $number in
0 ) echo "energy"    ;;  1 ) echo "touch" ;;
2 ) echo "climbing" ;;   3 ) echo "declare" ;;
4 ) echo "marry"  ;;     5 ) echo "relax"   ;;
6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;
8 ) echo "country" ;;    9 ) echo "folder" ;;
10 ) echo "individual" ;; 11 ) echo "youngest" ;;
12 ) echo "disco" ;; 13 ) echo "disturbed" ;;
14 ) echo "company" ;; 15 ) echo "scientific" ;;
16 ) echo "disaster" ;; 17 ) echo "protection" ;;
18 ) echo "curiously" ;; 19 ) echo "deranging" ;;
21 ) echo "facilities"

esac
 }

then i recalled it using the Dot command suggested here into my script as follow:

        . hangman_words
        word=$(stored_word)
        letters=$(echo $word | wc -c)
        letters=$(( $letters - 1 ))
        template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
        remaining=$letters

works fine, but the words should not be in a function or hard coded... and tbh i am not what i need to do, to create a word randomly, and sed and awk are out of question as well.

Can any one shed a light on this please?

K

number=$(( RANDOM % 21 + 1 ))
word=$( sed -n "$number p" words_list_file )
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
remaining=$letters

what can be used instead of sed, as i dont know anything about sed and rather learn shell scripting first and then look at sed, also just so i can understand this better, the random word file, needs to one word per line and numbered for this to work??

You are a star

Keyvan

You can store all the words in a file (assume words.txt).

stored_word()
{
   if [ -z "$stored_words_count" ]
   then
      stored_words_count=$(wc -l < words.txt)
   fi
   number=$RANDOM
   let "number = number % stored_words_count + 1"
   tail +$number words.txt | head -1
}

Another solution is to read the file into an array.

init_stored_words()
{
   while read w
   do
      words[${#words[@]}]=$w
   done < words.txt
   stored_words_count=${#words[@]}
}
stored_word()
{
   [ -z "$stored_words_count" ] && init_stored_words
   number=$RANDOM
   let "number %= stored_words_count"
   echo  ${words[$number]}
}

Jean-Pierre.

no=$(( RANDOM % 21 + 1 ))
while read str
do
	(( no = no - 1 ))
	if [[ $no -eq 0 ]]; then
		word=$str
		break
	fi
done < words_list_file 
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
remaining=$letters

am i correct to assume that the above script will produce a random word, as long as the words are set as follow:

[TEXT]
word1
word2
word3
word4
etc..
[/TEXT]

K

You are right. Keep each word in a separate line

I have a question or two to better understand this script:

1- $str=string???
2- how dose the script compare and reads the word line?

i hope the questions are not stupid, just trying to get a better understanding.

Thx

K

1.Do you mean that how str get the word from file?
While loop reads file line by line and str variable receive the word read from file

2.For each line read from file, variable no is decremented and when its value reaches zero, we reached the desired line in the file and assign value read to variable word and exit the loop.