Run Random Command in Bash Script

Run random command in bash script

-- Updated answer as per discussion in comments --

For Bash Users:

Code given in question works fine. Just don't forget to set permissions to +x (grants executable permission to all). Eg: chmod +x myscript.sh

Caution: Don't set RANDOM to a value. It looses its special properties.

Bash man page says-

RANDOM Each time this parameter is referenced, a random integer
between 0 and 32767 is generated. The sequence of random numbers may
be initialized by assigning a value to RANDOM. If RANDOM is unset, it
loses its special properties, even if it is sub- sequently reset.

For dash Users:

sh in ubuntu is a symbolic link to dash.
dash does not support RANDOM variable, it is just another variable.

If you are bound to use sh, you can use date function to generate
pseudo randoms only if your script is not time-dependent. (if your
script runs at strict intervals of time, it might produce same values)

RANDOM=`date '+%s'`
echo $(( $RANDOM % 12 )) # To generate random numbers less than 12

In General:

If you want to generate true random numbers use this-

dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" "

Source: http://www.linuxquestions.org/questions/programming-9/shell-script-random-variable-4088/

Generating random number between 1 and 10 in Bash Shell Script

$(( ( RANDOM % 10 )  + 1 ))

EDIT. Changed brackets into parenthesis according to the comment.
http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting

Random number from a range in a Bash Script

shuf -i 2000-65000 -n 1

Enjoy!

Edit: The range is inclusive.

Execute random file from a folder in a bash script

You can just use the "sort" tool with the option "-R" to sort randomly.
In a given directory you can run

ls -1 /etc/ | sort -R | head -1

to pick one file randomly out of all. The "etc" directory is just an example.

Your code should then look like this:

curl `ls -1 /my_folder/ | sort -R | head -1` &

But I don't understand how you call "curl" with a file? This will not execute the PHP code in the file.

How to generate random numbers between 0 and 1 in bash

As far as I can remember ${RANDOM} generates integers in the interval 0 - 32767. So, I guess, you might want to try something like this to generate random values in [0,1]:

bc -l <<< "scale=4 ; ${RANDOM}/32767"

Running a shell script once a day at random time

The best alternative to cron is probably at

  • See at man page

Usually, at reads commands from standard input, but you can give a file of jobs with -f.

Time wise, you can specify many formats. Maybe in your case the most convenient would be

  • at -f jobs now + xxx minutes

where your scripts gives xxx as a random value from 1 to 1440 (1440 minutes in a day), and jobs contains the commands you want to be executed.

How do I select a random file from a directory?

use the shuf command to create a random shuffle and head -1 to pick the top one

ls -1 *.png |  shuf | head -1

or

ls -1 *.png | shuf -n 1

Shuffle random list of COMMANDS in BASH?

You can pipe the shuffled commands to bash without any processing:

shuf commands.txt | bash


Related Topics



Leave a reply



Submit