Sponsored Content
Top Forums Shell Programming and Scripting Storing or Using Numeric Output From a Function Post 302539806 by PehJota on Monday 18th of July 2011 11:58:22 PM
Old 07-19-2011
Storing or Using Numeric Output From a Function

Hi all,

I'm trying to implement a linear congruential pseudorandom number generator (<http://en.wikipedia.org/wiki/Linear_congruential_generator>), since $RANDOM and /dev/random aren't standardized. I'm referring to the Shell & Utilities volume of POSIX.1-2008, but I'm running into some odd issues. This works fine:
Code:
#! /bin/sh

rand_x=1

rand()
{
    rand_x=$(((($rand_x * 1103515245) + 12345) % 4294967296))
    echo $rand_x
}

while true; do
    rand
    sleep 2
done

But I want to use the generated integer in an arithmetic expansion, for modular arithmetic (I know it's a sloppy way of limiting the range of values, but it's good enough for my purpose).

I've tried the following, but it simply repeats "1103527590 0" over and over:
Code:
#! /bin/sh

rand_x=1

rand()
{
    rand_x=$(((($rand_x * 1103515245) + 12345) % 4294967296))
    echo $rand_x
}

while true; do
    r=`rand`
    printf '%i %i\n' $r $(($r % 3))
    sleep 2
done

Similarly, this repeats "1103527590":
Code:
#! /bin/sh

rand_x=1

rand()
{
    rand_x=$(((($rand_x * 1103515245) + 12345) % 4294967296))
    echo $rand_x
}

while true; do
    printf '%i\n' `rand`
    sleep 2
done

It looks like, if I try to store or use the output of rand(), either $rand_x is being reset to 1 each time or the arithmetic substitution in rand() isn't being used.

But if I access $rand_x directly, as in the following script, I get the correct sequence:
Code:
#! /bin/sh

rand_x=1

rand()
{
    rand_x=$(((($rand_x * 1103515245) + 12345) % 4294967296))
}

while true; do
    rand
    printf '%i %i\n' $rand_x $(($rand_x % 3))
    sleep 2
done

However I'd like to avoid exposing $rand_x like that (rand() will eventually become a library function). So does anyone have any idea why using echo and capturing/using the output of rand() isn't working?

I'm testing this in GNU Bash and dash on GNU/Linux. When I get this working, I'll test it in more shells.

---------- Post updated at 23:58 ---------- Previous update was at 23:26 ----------

Quote:
Originally Posted by PehJota
It looks like, if I try to store or use the output of rand(), either $rand_x is being reset to 1 each time or the arithmetic substitution in rand() isn't being used.
The former seems to be the case. If I echo $rand_x from rand() and just store the output of rand(), $rand_x is reset to 1 for some reason.

The following script illustrates this:
Code:
#! /bin/sh

rand_x=1

rand()
{
    rand_x=$(((($rand_x * 1103515245) + 12345) % 4294967296))
    echo $rand_x >&2
    echo $rand_x
}

while true; do
    r=`rand`
    printf '%i %i\n' $rand_x $(($rand_x % 3))
    sleep 2
done

Note that in rand(), $rand_x is now also printed to standard error (file descriptor 2) so it appears on the terminal even though standard output is stored in $r.

Output (file descriptors 1 and 2):
Quote:
1103527590
1 1
1103527590
1 1
1103527590
1 1
...
Anyone have any idea why this is happening?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies

2. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

3. Shell Programming and Scripting

Using 'defaults read' and storing the output in a variable

Hi all, I'm creating a script which uses 'defaults read' to retrieve details from an Info.plist like this; defaults read "/Path/Contents/Info" CFBundleShortVersionString This works fine in Terminal and returns the expected values. Is it possible to use this command in a script, and... (0 Replies)
Discussion started by: davewg
0 Replies

4. UNIX for Dummies Questions & Answers

Storing a Function

Hi, I am running Sco Unix v3.2B. I want to know how can I store a function I create so I can use it later simply by just calling it from the command prompt. Sorry if I am asking such a noob question. Thanks. (7 Replies)
Discussion started by: sdilucca
7 Replies

5. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

6. UNIX for Dummies Questions & Answers

Storing lines of output into a script variable

I'm sure this is a simple thing but I can't figure it out. In a script that I'm writing, I'd like to be able to store each line of output from "ls -l" into a variable. Ultimately I'd like to end up with something like: for a in `ls -l` do something with $a doneBut that's reading each... (2 Replies)
Discussion started by: ewoods
2 Replies

7. Programming

Storing the output of a Unix cmd in my C program

Hello experts, How can I retrieve the output from a Unix command and use it as string variable in my C program? For example, when I issue the command 'date' I get: Tue Jun 11 09:54:16 EEST 2009 I do not want to redirect the output of the command to a file and then open the file from... (3 Replies)
Discussion started by: Goseib
3 Replies

8. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

9. Shell Programming and Scripting

sed not storing output in shell script

Hello, Following my learning of shell scripting.. I got stuck yet again. If I execute this command on terminal: $ sed "s/off/on/g" file > fileAUX I successfully change the text off to on from file to fileAUX. But the same command is not working inside a shell script. I tested and... (2 Replies)
Discussion started by: quinestor
2 Replies

10. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies
All times are GMT -4. The time now is 07:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy