Display shell online

Is there a way to get $random to work in shell? If I don't include bourne shell ( !#/bin/sh ) My program works.... however, I need to include !#/bin/sh in order to display my program on the internet.

#! /bin/sh or !# /bin/sh

Yeah I know that, but what I'm asking is why does $RANDOM not work when using !# /bin/sh in the header?

What shell do you usually use $RANDOM in? You should put in that shell instead of #!/bin/sh. On many platforms, /bin/sh is a very basic, minimal shell. Try 'echo $SHELL' inside your "good" shell to find out what it is.

If you have to stick with /bin/sh, you could use /dev/random as discussed here: Random Number with /bin/sh - Linux / UNIX Forum

Edit:

If you want to pick a value between 0 and 32767 (like the ksh/bash RANDOM does) then this is what you want:

echo `od -v -An -N2 -td < /dev/urandom`/2 | bc
1 Like

Thanks chubler, makes sense to me going to test it out right now

The fact the you state that /bin/sh is a Bourne shell would suggest that you are not on a GNU/Linux platform. In that case you may not have a /dev/random or /dev/urandom.

Although /dev/random and /dev/urandom are not POSIX, they are both available on HPUX and AIX, so he might have it.

$RANDOM does not exist in true Bourne Shell. It also does not exist in "csh" which could be more relevant to you.

If your normal shell is "csh" the "!#/bin/sh" syntax is valid but it goes off and starts a new Shell and then does nothing else. It does not execute the commands in the script after that line. When the "/bin/sh" session ends, it then continues with the next lines in the script but executes them in "csh" where $RANDOM is not valid.

The solution is to call a "sh" script from the "csh" script.

#!/bin/csh
# my_main_script.sh
/bin/sh my_sub_script.sh


#my_sub_script.sh
echo "${RANDOM}"