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)
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.
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)