LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-04-2007, 11:15 PM   #1
dtdionne
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Rep: Reputation: 0
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....
 
Old 09-05-2007, 12:45 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 09-05-2007, 03:53 AM   #3
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
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.
 
Old 09-05-2007, 08:28 AM   #4
dtdionne
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Original Poster
Rep: Reputation: 0
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.
 
Old 09-05-2007, 10:24 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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)

Last edited by colucix; 09-05-2007 at 10:28 AM.
 
Old 09-05-2007, 01:40 PM   #6
dtdionne
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Original Poster
Rep: Reputation: 0
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....
 
Old 09-06-2007, 02:04 AM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot set LD_LIBRARY_PATH in .cshrc (able to set other env variables) senthilpr_in Linux - Newbie 4 02-26-2007 01:46 PM
Scripting with pipes and Java BlueRaja Linux - Newbie 7 09-26-2006 06:53 AM
Scripting with pipes and Java BlueRaja Programming 1 09-24-2006 06:16 AM
(Bash Scripting) Another set of eyes please merana Programming 3 03-22-2005 05:14 AM
set and export using bash script acummings Linux - General 10 01-03-2005 03:22 PM


All times are GMT -5. The time now is 05:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration