randomization

I have two files:

First file with 10 words, as follow:

randomword1, randomword2, randomword3, randomword4, etc...

Second File shell script.

word=$(cat hangman_words | cut -d" " -f1)
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
echo $letters
echo $word

The script above will give always give me the first word in the file. I need it to choose a random one each time.

how can i randomly choose a diffrent one everytime i run a script. Hopefully with out using sed or awk.

thx in advance

K

You can do something like that :

random_word() 
{
   if [ -z "$all_words" ]
   then
      sed '1s/,/ /g' random_words_file | read all_words 
      set -A words -- ${all_words}
   fi  
   word_index=$(( $RANDOM % ${#words[@]} ))
   echo ${words[$word_index]}
}

word=$(random_word)

Jean-Pierre.

Jean i cant use sed in any of my scripting.. dont ask me why but thats the harsh truth. how can the above script be changed to remove the sed section?

thx mate

K

No homework questions on the forums.

jim,

np at all... i am trying to learn shell scripting and the only way for me to learn is to ask questions, and since i have no teachers or instructors to fall back on, then i can only use these forums to learn as much as i can. Please except my sincere apology if this post has offended any one.

K

you can use tr instead of sed :

      tr ',' ' ' < random_words_file | read all_words

Jean-Pierre.

gives this error

hang: line 36: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

K

The 'set -A' command is used to initialize an array : ksh specific
For bash try :

      declare -a words -- ${all_words}

For bourne shell : Arrays not supported

Jean-Pierre.

hang: line 36: declare: `--': not a valid identifier
hang: line 38: 27142 % 0 : division by 0 (error token is " ")

now it gives this error.. i think it might be the way i have set up the word file?

K

Try:

declare -a words ${all_words}

Jean-Pierre.

hang: line 38: 14663 % 0 : division by 0 (error token is " ")

now its getting silly :slight_smile:

K

Good practice in shell scripts is to declare the shell you want to use on line 1, with a she-bang #!

#!/bin/ksh      # korn shell
#!/bin/sh        # POSIX shell (can be any of several POSIX-compliant shells, includes bash )
#!/bin/bash     # bash               

echo $SHELL
will tell you what your current shell is.

oh how silly of me... that would have been usefull... sorry guys i should have mentioned it its bin/bash.. the error is still the same.

hang: line 38: 14663 % 0 : division by 0 (error token is " ")

K

bash is not very familiar to me, seems that arrays aren't managed in the same way than within ksh.

  • Add 'set -x' for debuging purpose
  • Change 'declare' statement and add array assigment
  • Read again the bash man pages :rolleyes:
random_word() 
{
   set -x
   if [ -z "$all_words" ]
   then
      sed '1s/,/ /g' random_words_file | read all_words 
      declare -a words
      words=( ${all_words} )
   fi  
   word_index=$(( $RANDOM % ${#words[@]} ))
   echo ${words[$word_index]}
}

word=$(random_word)

Jean-Pierre.