randomization


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting randomization
# 8  
Old 05-16-2007
The 'set -A' command is used to initialize an array : ksh specific
For bash try :
Code:
      declare -a words -- ${all_words}

For bourne shell : Arrays not supported

Jean-Pierre.
# 9  
Old 05-16-2007
Quote:
Originally Posted by aigles
The 'set -A' command is used to initialize an array : ksh specific
For bash try :
Code:
      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
# 10  
Old 05-16-2007
Try:
Code:
declare -a words ${all_words}


Jean-Pierre.
# 11  
Old 05-16-2007
Quote:
Originally Posted by aigles
Try:
Code:
declare -a words ${all_words}


Jean-Pierre.
hang: line 38: 14663 % 0 : division by 0 (error token is " ")

now its getting silly Smilie

K
# 12  
Old 05-16-2007
Good practice in shell scripts is to declare the shell you want to use on line 1, with a she-bang #!
Code:
#!/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.
# 13  
Old 05-16-2007
Quote:
Originally Posted by jim mcnamara
Good practice in shell scripts is to declare the shell you want to use on line 1, with a she-bang #!
Code:
#!/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

Last edited by keyvan; 05-16-2007 at 11:38 AM.. Reason: more information
# 14  
Old 05-16-2007
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 Smilie

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Randomization a matrix - perl / Shell

Hello all, I have a tricky question! (at least for me it is!). I'll try to explain it carefully here. Hope you can help me solving the whole or even parts of it! Here it is: I have a big input 0\1 table as a very simplified one is shown below: (The last row and column are the sum and... (0 Replies)
Discussion started by: @man
0 Replies
Login or Register to Ask a Question