LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-12-2015, 11:46 AM   #1
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Rep: Reputation: Disabled
help with random function in Linux BASH


Hi Guys
i have a script that have variable called :
NIO=4
and i want the random function to own that variable inside it such as below :
D=$(cat /dev/urandom | tr -dc '0-$NIO' | fold -w 1 | head -n 1)

as u see i want to set range from 0-4
but i dont want to set 4
i want to set it as variable


the problem here is the random function dont understand the syntax above


hope to help me

cheers
 
Old 12-12-2015, 12:59 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
The $RANDOM variable in bash generates a pseudo random number every time it is referenced. You could use modulo to force the number to always be between 0 and 4.

Code:
D="$(( $RANDOM % 4 ))"
0-4 excluding 4. Use 5 instead if you want to include 4. If you want to use NIO to specify the max number like in your original post you could do:

Code:
DIO=4
D="$(( $RANDOM % ($NIO + 1) ))"

Last edited by sag47; 12-12-2015 at 01:25 PM.
 
1 members found this post helpful.
Old 12-12-2015, 01:04 PM   #3
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
The $RANDOM variable in bash generates a pseudo random number every time it is referenced. You could use modulo to force the number to always be between 0 and 4.

Code:
NIO="$(( $RANDOM  4 ))"
edit: For some reason LQ keeps stripping my percent symbol from the posted equation.

thansk , but im forced to work on the scenario above as the fourmaula above :
i have
D=$(cat /dev/urandom | tr -dc '0-4' | fold -w 1 | head -n 1)

wnt to replace it with
D=$(cat /dev/urandom | tr -dc '0-$NIO' | fold -w 1 | head -n 1)

can u hep me ?
 
Old 12-12-2015, 01:08 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by dr.x View Post
thansk , but im forced to work on the scenario above as the fourmaula above :
i have
D=$(cat /dev/urandom | tr -dc '0-4' | fold -w 1 | head -n 1)

wnt to replace it with
D=$(cat /dev/urandom | tr -dc '0-$NIO' | fold -w 1 | head -n 1)

can u hep me ?
I updated my original post. It has the correct equation.

Why are you "forced" to work with that "specific scenario"? Project constraints don't normally require a specific answer but to solve a problem. The answer I gave you does what you want in a simpler manner: generate a number between 0-4.

Single and double quotes have special meaning in bash. See "Quoting" section of the bash man page. tr -dc '0-$NIO' has single quites so it is using the literal characters $NIO instead of what you probably want (it to be interpreted as an environment variable). Use double quotes instead.

What are you actually trying to accomplish? What's then end goal? The real problem you're trying to solve?

Last edited by sag47; 12-12-2015 at 01:21 PM.
 
1 members found this post helpful.
Old 12-12-2015, 01:13 PM   #5
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
I updated my original post. It has the correct equation.

Why are you "forced" to work with that "specific scenario"? Project constraints don't normally require a specific answer but to solve a problem. The answer I gave you does what you want in a simpler manner: generate a number between 0-4.

What are you actually trying to accomplish? What's then end goal? The real problem you're trying to solve?

im really appreciating ur time so so so much .
i have already a script with long lines that i dont want to change .

i tried ur soltution , but it didnt work .
see below :
NIO="$(( $RANDOM % 4 ))"
[root@testvmpackagevpn ~]# D=$(cat /dev/urandom | tr -dc '0-$NIO' | fold -w 1 | head -n 1)
tr: range-endpoints of `0-$' are in reverse collating sequence order

as u see the NIO value is not recognized as we need

?!
 
Old 12-12-2015, 01:27 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by dr.x View Post
im really appreciating ur time so so so much .
i have already a script with long lines that i dont want to change .

i tried ur soltution , but it didnt work .
see below :
NIO="$(( $RANDOM 4 ))"
[root@testvmpackagevpn ~]# D=$(cat /dev/urandom | tr -dc '0-$NIO' | fold -w 1 | head -n 1)
tr: range-endpoints of `0-$' are in reverse collating sequence order

as u see the NIO value is not recognized as we need

?!
No, replace D. See my original post again. You're still using single quotes.

Read the bash man page on Quoting, RANDOM environment variable, and Arithmetic Expansion to better understand my solution.

Also, if you're afraid to lose changes when changing your script consider using git version control. It tracks history as you change scripts.

Last edited by sag47; 12-12-2015 at 01:37 PM.
 
1 members found this post helpful.
Old 12-13-2015, 12:38 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
NIO=4
D=$(cat /dev/urandom | tr -dc "0-$NIO" | fold -w 1 | head -n 1)
try this.
 
1 members found this post helpful.
Old 12-13-2015, 06:16 PM   #8
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Cat abuse alert.
 
1 members found this post helpful.
Old 12-14-2015, 12:55 AM   #9
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
Hi Guys

you were both nice

the issue is resolved and that u so so so much


my best regards
 
Old 12-14-2015, 04:08 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
please share your solution.
others will benefit.

ps:
Quote:
Originally Posted by berndbausch View Post
Cat abuse alert.
cat abuse?
 
Old 12-14-2015, 04:14 PM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
from the Artist formerly known as "The useless use of cat"
 
  


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
[SOLVED] Bash Problems - Random Headers when starting bash in X androidd Slackware 3 01-13-2011 04:29 PM
looking for a linux audio player that uses the C language rand() function for random dateofexpiration Linux - Software 1 08-14-2007 07:29 PM
Fast random number function Creep Programming 2 03-07-2004 09:37 AM
bash random function NSKL Programming 6 02-02-2003 01:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:11 PM.

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
Open Source Consulting | Domain Registration