$RANDOM does not work inside a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $RANDOM does not work inside a shell script
# 1  
Old 07-13-2010
$RANDOM does not work inside a shell script

Hi folks

I'm coding on Ubuntu 9.04 standard shell.

I'm writing a script that needs to generate a random number at some point of its execution.

When I do
Code:
echo $RANDOM

as a command inside shell, I clearly get some randomly generated number

However when I do
Code:
i=`$RANDOM`
echo $i

or even
Code:
echo $RANDOM

as lines inside a shell script that I later run, I get nothing. Both options just give me a blank.

What am I doing wrong?
# 2  
Old 07-13-2010
Try

Code:
i=$(echo $RANDOM)
echo $i

Guru.
# 3  
Old 07-13-2010
No luck. Same output
# 4  
Old 07-13-2010
Hi

RANDOM is not available in all shells. I know for sure it is there in ksh/sh,not there in csh/tcsh. Since its working on your prompt, try to run your script in the same shell as is your command line.

Guru.
# 5  
Old 07-13-2010
RANDOM wasn't available in Bourne Shell.
# 6  
Old 07-16-2010
Hey guys

So I've just moved to a new machine, and reinstalled Ubuntu 10.04 from scratch. I first tried
Code:
$ echo $RANDOM

in the shell. It works.
Then, when I tried the same command as a one line shell script called test.sh, and ran
Code:
$ sh test.sh

My output was a blank line.

So RANDOM is clearly available in this shell. Just that it doesn't work inside a script. Now what?
# 7  
Old 07-16-2010
In Ubuntu sh is symlinked to dash.

Code:
d@DeCoBuntu:~$ ls  -ltrh /bin/sh
lrwxrwxrwx 1 root root 4 2010-05-26 02:41 /bin/sh -> dash
d@DeCoBuntu:~$ ^C

however, if you're just looking for some random numbers and letters, you can do this in pretty much any shell:

Code:
d@DeCoBuntu:~$ type rand
rand is a function
rand () 
{ 
    tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c$1;
    echo
}

Less efficient, but it'll get the job done.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching user inside a shell script doesn't seem to work

Linux version : Oracle Linux 6.4 Shell : Bash The following script will be run as root. During the execution, it should switch to oracle user and execute few commands. After googling and searching within unix.com , I came up with the following syntax ## Enclosing all commands in double... (7 Replies)
Discussion started by: John K
7 Replies

2. Shell Programming and Scripting

Shell script that runs a random shell script

Hi, im trying to make a shell script that basically runs a random shell script form a list of shell scripts i specify. Im not very good at writing shell scripts, and am new to linux. Thanks in advance :) (15 Replies)
Discussion started by: kylecn
15 Replies

3. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

4. UNIX for Dummies Questions & Answers

A shell script or software for generating random passwords

Hi, Is there an shell script/batch file to genarate random passwords which expires after a stipulated time period? Please suggest a software which does this for AIX and windows both else. Thanks. (5 Replies)
Discussion started by: dwiravi
5 Replies

5. UNIX for Dummies Questions & Answers

A shell script or any software to genarate random passwords

Hi, Is there a shell script or any software to genarate random passwords and the passwords expire automatically after a stipulated time period. Please suggest. (2 Replies)
Discussion started by: dwiravi
2 Replies

6. Shell Programming and Scripting

how do i generate random integer using only shell script

Hi All, I need to generate 4 digit random no using only shell script. Please help in this ASAP. Thanks in advance... Regards, sridhar. (1 Reply)
Discussion started by: sridhusha
1 Replies

7. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

Random files do not FTP in the shell script

The following script is used to loop through files in the /tmp directory and transfer those files onto another server. However, some of the files do not transfer. It is very random when the transferring is done (i.e. one of the files won't transfer then next time, that one will transfer and... (1 Reply)
Discussion started by: RLatham20
1 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question