LinuxQuestions.org
Visit Jeremy's Blog.
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, 10: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-04-2007, 11:45 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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, 02:53 AM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
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, 07: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, 09:24 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

Rep: Reputation: 31
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 12:46 PM
Scripting with pipes and Java BlueRaja Linux - Newbie 7 09-26-2006 05:53 AM
Scripting with pipes and Java BlueRaja Programming 1 09-24-2006 05:16 AM
(Bash Scripting) Another set of eyes please merana Programming 3 03-22-2005 04:14 AM
set and export using bash script acummings Linux - General 10 01-03-2005 02:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration