$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
# 8  
Old 07-16-2010
At your normal prompt where $RANDOM is working:
Code:
echo $SHELL

Whatever that comes out try putting that in the shebang line in your script.
This User Gave Thanks to methyl For This Post:
# 9  
Old 07-16-2010
try:

bash test.sh
or
ksh test.sh
# 10  
Old 07-16-2010
Thanks. It fixes the immediate problem, but I'm curious as to why the RANDOM variable doesn't work inside the script, and I didn't fully understand the symlink explanation. Can you elaborate?
# 11  
Old 07-16-2010
To paraphrase the earlier poster, when you type "sh" in your particular version of Linux you actually get "dash" because "sh" is a symbolic link to "dash".
In your case you probably needed to specify "bash".

In older unixes "sh" gave you the Bourne Shell but in most modern mainstream unixes "sh" gives you the Posix Shell. There is even more variation in Linux.
# 12  
Old 07-18-2010
I'm not sure what the problem is. I'm using /bin/bash, and doing:

Code:
#!/bin/bash
i=$RANDOM
echo $i
i=`expr $i + 1`
echo $i

Works just fine..

---------- Post updated at 09:45 PM ---------- Previous update was at 09:45 PM ----------

I'm not sure what the problem is. I'm using /bin/bash, and doing:

Code:
#!/bin/bash
i=$RANDOM
echo $i
i=`expr $i + 1`
echo $i

Works just fine..
# 13  
Old 07-18-2010
@methyl - Thanks

@eclipseagent - My code was the same as yours and wasn't working. I finally realized that I had been forgetting to do `chmod +x <filename>'! It works now. Thanks for helping me work out such an obvious error :P
# 14  
Old 07-18-2010
Quote:
Originally Posted by ksk
@methyl - Thanks

@eclipseagent - My code was the same as yours and wasn't working. I finally realized that I had been forgetting to do `chmod +x <filename>'! It works now. Thanks for helping me work out such an obvious error :P
Actually.. i looked at yours and you had backticks ( ` ) in `$RANDOM`, which means you're trying to run the command.. which you wouldn't want to do.
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