Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

With a "normal" (I mean full) Linux distro, it works just fine:

sleep $(echo "$[ ($RANDOM % 10 ) ]")

OK, it waits for about 0-9 seconds.

but under OpenWRT [not using bash, but rather ash]:

$ sleep $(echo "$[ ($RANDOM % 9 ) ]")
sleep: invalid number '$['
$

and why:

$ echo "$[ ($RANDOM % 9 ) ]"
$[ ( % 9 ) ]
$ 

So does anyone have a way to generate random numbers under OpenWRT, so I can put it into the "sleep"?

share|improve this question
    
ash isn't bash; you can't use bash features with it. – Ignacio Vazquez-Abrams Jan 13 '11 at 0:10
    
@user62367 someone from SO has already answer your question. – greatwolf Jan 13 '11 at 11:52
1  
Seriously, stop crossposting. – Wuffers Feb 5 '11 at 15:41

Ash doesn't understand $[] and it's deprecated in Bash. Also, the echo is unnecessary. Use this instead for both Bash and Ash:

sleep $(( $RANDOM % 10 ))
share|improve this answer
up vote -1 down vote accepted

Here's an (unreliable) way to produce an integer between 0 and 9 (inclusive):

sleep $(head -30 /dev/urandom | tr -dc "0123456789" | head -c1)

This reads 30 lines from /dev/urandom, uses tr to discard all characters that aren't digits, and then grabs only the first character of the remaining digits.  This is unreliable insofar as there is a possibility (with very low probability) that /dev/urandom will yield 30 lines that happen to contain no digits.

This can be extended to generate a three-digit number (in the range 0-999) as follows:

$(head -30 /dev/urandom | tr -dc "0123456789" | head -c3)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.