This is in response to a private message I received (since I can't send PMs until I have 10 posts...).
I've implemented this linear congruential generator in C to generate the correct sequence of integers (that is, what I'm expecting from this shell function):
And here is the printed sequence:
So it's generating the correct number. I guess the problem is that this number is 32 bits long and would exceed a signed long integer on my i686 system. Oops.
I changed my modulus, multiplier, and increment so the returned value doesn't exceed 2^31. So now this works:
For some reason "echo" still just resets $rand_x to its original value... I wish I knew why it did that. But I'll just use "return".
I'll test this script in a few different shells now to see if the signed long issue affects numbers before they're wrapped with modulo. If not, I think this problem is solved.
EDIT:
Harish: I know it's an infinite loop. I just interrupt it after a few iterations.
Ygor: Oh, I didn't realize command substitution used a subshell (I see it now in POSIX.1-2008) or that variables are set differently in a subshell. Yeah, that explains the weird behavior with "echo" then. Thanks!
EDIT 2:
I used these new values in three different shells so far (GNU Bash, dash, and BusyBox ash) and in C (modifying the above code). I generated 1000 pseudorandom numbers using all four test cases, and all four results were equal. I think the results are undefined if there is overflow in the intermediate arithmetic though. Oh well; I'll test more shells to be sure.
EDIT 3:
Well I've tested 8 shells (GNU Bash, dash, BusyBox ash, zsh, csh, tcsh, ksh, and mksh) across two PC architectures, haha. All give the same 1000 numbers (seeded at 1), so the arithmetic seems portable. Anyway, thanks again Ygor and Harish!
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)