how to generate random numbers in a shell script?
Florian Diesch
diesch at spamfence.net
Tue Oct 6 22:56:28 UTC 2009
Eugeneapolinary Ju <eugeneapolinary81 at yahoo.com> writes:
> # something=$[ ($RANDOM % 59 ) ]
> # echo $something
> 29
>
>
> ok, but if I put it in a script:
>
> # echo -e '#!/bin/bash\nsomething=$[ ($RANDOM % 59 ) ]'>test.sh
>
> # cat test.sh
> #!/bin/bash
> something=$[ ($RANDOM % 59 ) ]
>
> # sh test.sh
> test.sh: 2: Syntax error: "(" unexpected
>
>
>
> Why??
sh doesn't support $[] (and in bash it's deprecated), and $RANDOM is
just a normal varobnale that doesn't create random numbers.
Use either
bash test.sh
or make your script executable:
chmod u+x test.sh
and execute it using
./test.sh
Florian
--
<http://www.florian-diesch.de/doc/emacs/>
More information about the ubuntu-users
mailing list