The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
$random relle Shell Programming and Scripting 6 07-11-2008 02:59 AM
Random command melaz Shell Programming and Scripting 1 07-10-2008 05:24 PM
FTP random files whegra Shell Programming and Scripting 3 11-21-2007 01:22 PM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 01:03 AM
random in ksh pascalbout AIX 1 01-04-2006 06:53 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 14
Random

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

I currently have this function:

Code:
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:

Code:
        . 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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
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
Reply With Quote
  #3 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 14
Quote:
Originally Posted by anbu23
Code:
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

Last edited by keyvan; 05-24-2007 at 05:25 AM. Reason: more information
Reply With Quote
  #4 (permalink)  
Old 05-24-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,199
You can store all the words in a file (assume words.txt).
Code:
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.
Code:
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.

Last edited by aigles; 05-24-2007 at 09:31 AM. Reason: Modify number calculation for tail/head solution.
Reply With Quote
  #5 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by keyvan
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
Code:
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
Reply With Quote
  #6 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 14
Quote:
Originally Posted by anbu23
Code:
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
Reply With Quote
  #7 (permalink)  
Old 05-24-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by keyvan
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0