LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash, Env, Set, Export, Pipes....Scripting (http://www.linuxquestions.org/questions/programming-9/bash-env-set-export-pipes-scripting-582260/)

dtdionne 09-04-2007 10:15 PM

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

Why doesn't this work:

CASE #1
[slammer]# perl getlinks.pl -d http://knihovny.cvut.cz/ftp/pub/vmware/ . '\.gz$' | grep any-any | (read any; export any)
[slammer]# echo $any

[slammer]#

nor this:

CASE #2
[slammer]# perl getlinks.pl -d http://knihovny.cvut.cz/ftp/pub/vmware/ . '\.gz$' | grep any-any | read ANY; export ANY=$ANY
[slammer]# echo $ANY

[slammer]#

nor this:

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

[slammer]#

nor this:

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

[slammer]#


given the following:

CASE #1 and CASE #2
[slammer]# perl getlinks.pl -d http://knihovny.cvut.cz/ftp/pub/vmware/ . '\.gz$' | grep any-any | (read any; echo $any)
http://knihovny.cvut.cz/ftp/pub/vmwa...date113.tar.gz

and this:

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


Thanks....

wjevans_7d1@yahoo.co 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:
Code:

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

wjevans_7d1@yahoo.co - 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 value...as 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 missing....it's really starting to piss me off.

colucix 09-05-2007 09:24 AM

From Advanced Bash-Scripting Guide, Chapter 4
Quote:

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
Code:

perl getlinks.pl -d http://knihovny.cvut.cz/ftp/pub/vmware/ . '\.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 getlinks.pl -d http://knihovny.cvut.cz/ftp/pub/vmware/ . '\.gz$' | grep any-any`
[slammer]# echo $any
http://knihovny.cvut.cz/ftp/pub/vmwa...date113.tar.gz

Oh the power of the back tick, behold....

wjevans_7d1@yahoo.co 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 03:19 AM.