11/25/20 8:38AM
I recently ran into an environment variable "RANDOM" in Bash(a command shell that comes with some operating systems), this environment variable returns a random integer number between 0 and 32767. I wrote a Bash script to return at most 3 random integer numbers at a time, each of which is between 1 and a given upper-limit integer number up to 32768, here is the content of the script:if(($#==0));then
echo "arguments expected"
exit
fi
if(($#>3));then
echo "too many arguments"
exit
fi
p='^[0-9]+$'
for i in $@; do
if ! [[ $i=~$p ]];then
echo "$i is not a number"
exit
fi
if(($i<1));then
echo "number $i is too small"
exit
fi
if(($i>32768));then
echo "number $i is too big"
exit
fi
done
for i in $@; do
echo $(($RANDOM%$i+1))
done
Here is how I invoke the script in the Bash command line to generate a random integer number between 1 and 12:
$ bash r.bash 12
10
| Feng Zhou |
About Us|Corporate Responsibility|Contact|Advertising Program
© 2026 Sretto