LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-02-2003, 10:23 AM   #1
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Rep: Reputation: 46
bash random function


I want to write a simple bash script that, when executed, will pick a random of 5 colors and start a aterm with that color.

So far i have just defined the vars:
RED="aterm -tint red"
GREEN="aterm -tint green"
and so on for every tint color, you get the point.
Now is there a random function that i can use, that will choose one of the variables and execute it? I know something like this exists in Perl, but is there a easy way in bash?
Thank you.
-NSKL
 
Old 02-02-2003, 12:58 PM   #2
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
from the Bash manpage:

,----
| Shell Variables
| The following variables are set by the shell:
|
| [. . .]
|
| 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.
`----

so, just do this:

$ echo $RANDOM
 
Old 02-02-2003, 01:50 PM   #3
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Original Poster
Rep: Reputation: 46
Thanks for help, but i never read anything about bash scriping, just looked at some scripts and tried to figure out how it works, which is pretty much like any programming lang, but im still very newbie...
So, i have defined my vars:

RED="aterm -options"
GREEN="aterm -options"
BLUE="aterm -options"
CLEAR="aterm -options"
YELLOW="aterm -options"

now how would i go about using echo $RANDOM to pick one of the 5 vars, and execute it?
Thanks in advance
-NSKL
 
Old 02-02-2003, 02:03 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,666
Blog Entries: 54

Rep: Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952
case ${RANDOM:0:1} in
0) RED="aterm -tint red";;
1) GREEN="aterm -tint green";;
# etc etc
esac

*search TLDP for the "Advanced bash Scripting Guide".
 
Old 02-02-2003, 02:11 PM   #5
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Original Poster
Rep: Reputation: 46
Thank you, i'll invest in a good book too...
-NSKL
 
Old 02-02-2003, 02:34 PM   #6
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
Why not try something along these lines:

Code:
#!/bin/bash

array=(\
	"echo 1" \
	"echo 2" \
	"echo 3" \
	"echo 4" \
	"echo 5")

exec ${array[$((RANDOM %= $((${#array[@]}))))]}
each time you execute the script, a random command from the array
will be executed

Last edited by leed_25; 02-02-2003 at 03:15 PM.
 
Old 02-02-2003, 02:39 PM   #7
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
I like unspawn's better. I didn't know about the 0:1 thang.

edit::

the 0:1 thang just selects the first character of the
random number string generated by $RANDOM --I had to
go read about it in the manpage-- whereas the %=
model generates a random number in the range of
posssible array elements. Take yer pick.

The 0:1 model is much simpler. I still like it.


Last edited by leed_25; 02-02-2003 at 03:03 PM.
 
  


Reply


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 - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 02:25 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 07:05 AM
BASH script showing random behavior chatterbug89 Programming 12 07-19-2005 11:45 PM
Bash function pazvant Linux - Software 1 03-08-2005 08:47 AM
Fast random number function Creep Programming 2 03-07-2004 10:37 AM


All times are GMT -5. The time now is 02:20 AM.

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