From: "Conway S. Smith" <beolach@comcast.net>
To: zavandi <zavandi@gmail.com>
Cc: linux-newbie@vger.kernel.org
Subject: Re: bash problem
Date: Tue, 01 Mar 2005 17:45:27 -0700 [thread overview]
Message-ID: <42250CA7.6080103@comcast.net> (raw)
In-Reply-To: <321aa0d20503011009d1b9060@mail.gmail.com>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
zavandi wrote:
> 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?
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 majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
next prev parent reply other threads:[~2005-03-02 0:45 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-03-01 18:09 bash problem zavandi
2005-03-02 0:45 ` Conway S. Smith [this message]
2005-03-02 9:36 ` zavandi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=42250CA7.6080103@comcast.net \
--to=beolach@comcast.net \
--cc=linux-newbie@vger.kernel.org \
--cc=zavandi@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox