Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-15-2011, 12:09 AM   #1
Senior Member
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
Question BASH Script and $RANDOM

Inspired by this thread, I wrote a small BASH script just for the hell of it.

Anyway, take a look at it first:
#! /bin/bash

while (true)
let C++
if [ $A -eq $B ]; then
echo "Incredible! The cosmos have bestowed $A upon you as your lucky number."
echo "Your UNIX system selected the same number twice consecutively from the boundless pool of '/dev/random'. This is a highly improbable feat; you are truly blessed!"
echo "…of course, it took $C attempts to do it. :P"
Two questions:
  • Is it possible for the script to ever break the loop?
  • Is there anything wrong (syntactically and/or logically) with the script?
Thanks for all help in advance; it is much appreciated even if the script is just for giggles.

Last edited by lupusarcanus; 03-15-2011 at 12:12 AM. Reason: English errors fixed.
Old 03-15-2011, 02:21 AM   #2
LQ Guru
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
First of all, I would like to say thanks, as my thread inspired you to do this LOL

But seriously now, I have to run this and time it once I get home to see, if it would ever break, how LONG this would take. Thanks for giving me something to do with my time!

And also, if I think of anything to make it better, I will let you know.

Old 03-15-2011, 02:24 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Based on the following:
$RANDOM is an internal Bash function (not a constant) that returns a pseudorandom [1] integer in the range 0 - 32767.
Yes to eventually being equal at least after this maximum is reached.

As to:
Is there anything wrong (syntactically and/or logically) with the script?
1. Zero indenting which makes it quite difficult to read
2. C is an integer and not a string so quoting would not seem appropriate although it of course causes no error
3. (()) is used by bash for numeric comparisons and arithmetic so the following changes could be made:
if [ $A -eq $B ]; then

if (( A == B )); then

let C++

4. Personally I am not fond of infinite loops so I would probably have an actual test in the while loop.


bash scripting, infiniteloops, logic, random number, syntax

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script? how to rename files in random way? lefty.crupps Linux - Software 16 11-03-2015 02:07 PM
[SOLVED] Bash Problems - Random Headers when starting bash in X androidd Slackware 3 01-13-2011 04:29 PM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
BASH script showing random behavior chatterbug89 Programming 12 07-19-2005 10:45 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration