Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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:
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)
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?
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.