magna_nz
magna_nz

Reputation: 1273

creating random number in range and accessing arrayindex char

I'm trying to get a random character print out in an array that is stored with characters. I'm new to bash and been looking at some guides along the way but have run into a bit of a problem.

I create an array to test with which stores chars. I then have a variable called Range which is 21 (this is the length of the array).

 #create an characters to test with
 letters=(a,b,e,g,i,j,k,l,m,n,p,q,r,s,t,u,v,y,x,w,z)

 RANGE=21

I then try and get a random number from 0 -> range, so it will correspond with array indices. I then need to access the array index of the random number and store that in a variable called arraychar. I have done it like this:

number=$RANDOM%$RANGE #gets int of a random number between 0 and range (size of array)
arraychar=${letters[$number]} #chooses a random char from array and stores in arraychar

Then I want to pass this letter as an argument to my executable like so:

for i in `seq 1 3`;do
   echo $i " "
   ./sample-tree -$arraychar
done

It keeps hanging here, and I'm not too sure what's it doing, and why it isn't working.

Any help would be appreciated as I'm a newbie!

Thanks.

Upvotes: 0

Views: 83

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

There are a number of things wrong with the code snippets in your post.

letters=(a,b,e,g,i,j,k,l,m,n,p,q,r,s,t,u,v,y,x,w,z) is an array of one element. See what declare -p letters spits out at you to confirm. sh arrays are whitespace separated lists.

The length of an array in the shell is available via ${#letters[@]}.

number=$RANDOM%$RANGE is not doing what you expect it is. Again see what declare -p number says the value of the variable is (echo "$number" would work too).

You need arithmetic expansion to evaluate the modulus operation (i.e. number=$((RANDOM % RANGE))).

A couple other "stylistic" points as well:

  • You don't need $ in the array indexing context (e.g. ${letters[$number]} can be written as ${letters[number]}).
  • In bash (and many other shells) you can use {1..3} to get that list of numbers without needing a sub-shell and seq.
  • The trailing space in echo $i " " isn't going to do much for you because echo outputs a newline as well (unless you use -n but in that case you might just be better off with printf).

Upvotes: 2

Related Questions