Skip to contentSkip to conversation

bash problem

DISCUSSION zavandi 2 participants 3 messages Mar 1, 2005 – Mar 2, 2005

lore
Share & tools
lore

zavandi

author
#0 lore raw

I found some strange result. Here it is in its most simplified form I can think of.

This command usually prints a few OKs, as expected:

for ((i=0; i< 100000; i++)); do if [ $RANDOM -eq 0 ]; then echo OK; fi; done

But this other command _never_ prints anything:

for ((i=0; i< 100000; i++)); do if [ $RANDOM -eq 0 ]; then echo OK; fi; done | cat

Why?
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 5 lines · zavandi

I found some strange result. Here it is in its most simplified form I can think of.

This command usually prints a few OKs, as expected:

Quoted 8 lines

for ((i=0; i< 100000; i++)); do if [ $RANDOM -eq 0 ]; then echo OK; fi; done

But this other command _never_ prints anything:

for ((i=0; i< 100000; i++)); do if [ $RANDOM -eq 0 ]; then echo OK; fi; done | cat

Why?

Interesting.... semi-educated wild guess: when you pipe two commands together, the bash manpage says each command is executed as a seperate process (i.e., in a subshell). It also says If RANDOM is unset, it loses its special properties, even if it is subsequently reset. So, my guess is the subshell your for loop runs in had its RANDOM unset (or possibly was never set).

One thing that backs this up, is that enclosing the for loop in parenthesis, which also makes it execute in a subshell, like so

(for ((i=0; i<100000; i++)); do
if [ $RANDOM -eq 0 ]; then

    echo OK;

fi;
done)

Behaves the same way as piping it through cat.

for ((i=0; i<100000; i++)); do
if [ $RANDOM -eq 0 ]; then

    echo OK;

fi;
done | cat

But, when I create a shell script file that only contains the for loop, and execute it in a subshell (either by enclosing it it parenthesis or piping it through cat), it works correctly:

~ $ cat random.sh
#!/bin/bash
for ((i=0; i<100000; i++)); do
if [ $RANDOM -eq 0 ]; then

    echo OK;

fi;
done

~ $ (./random.sh)
OK
OK
OK
OK
~ $ ./random.sh | cat
OK
OK
OK
OK

So, that means either I'm wrong and it's not something to do with the for loop being executed in a subshell, or else the bash script sets up the subshell's environment so that RANDOM works correctly.

Anyway, good luck with whatever you're doing that needs this. Conway S. Smith
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFCJQyjGL3AU+cCPDERAucAAKDTSS+hYSQNlZIn1QyJGeW4Km10owCg5n4C rf6aENxC5xugJ2M9WteAH9w=
=SE7u
-----END PGP SIGNATURE-----
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

author
#2 lore raw
Quoted 4 lines · Conway S. Smith

[...]
So, that means either I'm wrong and it's not something to do with the for loop being executed in a subshell, or else the bash script sets up the subshell's environment so that RANDOM works correctly.

It could very well be about the for loop being executed in a subshell, but it's also about the particular number 0. If I replace "0" with "1" (or any other 15-bit int) it works correctly.

Quoted 1 line

Anyway, good luck with whatever you're doing that needs this.

Actually, I was trying to find the maximum value for $RANDOM. Then I read the man page and remembered I was never getting the minimum value.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs