-   Programming (
-   -   Bash, Env, Set, Export, Pipes....Scripting (

dtdionne 09-04-2007 10:15 PM

Bash, Env, Set, Export, Pipes....Scripting

Why doesn't this work:

[slammer]# perl -d . '\.gz$' | grep any-any | (read any; export any)
[slammer]# echo $any


nor this:

[slammer]# perl -d . '\.gz$' | grep any-any | read ANY; export ANY=$ANY
[slammer]# echo $ANY


nor this:

[slammer]# ls | grep vmware-any-any | (read old; export old)
[slammer]# echo $old


nor this:

[slammer]# ls | grep vmware-any-any | (read old; export old=$old)
[slammer]# echo $old


given the following:

CASE #1 and CASE #2
[slammer]# perl -d . '\.gz$' | grep any-any | (read any; echo $any)

and this:

CASE #3 and CASE #4
[slammer]# ls | grep vmware-any-any | (read old; echo $old)

Thanks.... 09-04-2007 11:45 PM

When you pipe things from one point to another, each guy is running in his own process. This means that variables defined in one of these processes won't have their definitions brought back to the parent proceess.

This is the way to do what you want:

ANY=$(fred | grep barney)
echo $ANY    # or whatever

Hope this helps.

reddazz 09-05-2007 02:53 AM

Moved: This thread is more suitable in the Programming Forum and has been moved accordingly to help your thread/question get the exposure it deserves.

dtdionne 09-05-2007 07:28 AM - i know the basics of env var's, and what you have suggested will not solve the problem I am having. Also, as you can see from my examples, piping inside of parens keeps them all inside of the first child which is evident in the fact that at the end of the run, i can echo the correct well, from everything I have read, exporting is supposed to make variable changes much more permenant, remaining for the life it's parent shell.

So, once more, please someone, tell me what in the hell it is that I am's really starting to piss me off.

colucix 09-05-2007 09:24 AM

From Advanced Bash-Scripting Guide, Chapter 4

A script can export variables only to child processes, that is, only to commands or processes which that particular script initiates. A script invoked from the command line cannot export variables back to the command line environment. Child processes cannot export variables back to the parent processes that spawned them.
It looks like we can only do in two-steps

perl -d . '\.gz$' | grep any-any > /tmp/somefile
export ANY=$(cat /tmp/somefile)

dtdionne 09-05-2007 12:40 PM

yeah, I had that working.

Well I went back and thought about what wjevans said and it hit me....

[slammer]# any=`perl -d . '\.gz$' | grep any-any`
[slammer]# echo $any

Oh the power of the back tick, behold.... 09-06-2007 01:04 AM

Might I make a suggestion?

Instead of saying `blah`, say $(blah).

There are no advantages to back ticks. The $() syntax has the advantage that it can be nested.

All times are GMT -5. The time now is 02:54 AM.