LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-15-2007, 06:36 PM   #1
Elric of Grans
Member
 
Registered: May 2005
Posts: 46

Rep: Reputation: 15
Shell scripting, capturing the output...


I am having a little trouble with something I am trying to do with shell scripting. I would ultimately like to export the pid of a specified process to a specific environmental variable, but I seem to be having a spot of difficulty in just capturing the output.

I can get the pid with this:
ps -A | grep $program | awk '{print $1}'

This finds the correct program and prints its pid. I cannot for the life of me work out how to put this into an environmental variable, however; indeed, I cannot even work out how to capture it! I attempted to put it into a regular variable with result=`awk '{print $1}'`, but that does not seem to do anything. I have also tried several silly things, grasping at straws, but nothing seems to work. Is it possible for me to catch the result of awk and export it so that another program in the same environment can snatch it up?
 
Old 02-15-2007, 07:13 PM   #2
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Rep: Reputation: 15
var=$(ps -A | grep $program | awk '{print $1}')
 
Old 02-15-2007, 07:23 PM   #3
Elric of Grans
Member
 
Registered: May 2005
Posts: 46

Original Poster
Rep: Reputation: 15
Ahh, so you need to make it the result of the whole block rather than just the final program! Thanks for the help there
 
Old 02-15-2007, 07:48 PM   #4
Elric of Grans
Member
 
Registered: May 2005
Posts: 46

Original Poster
Rep: Reputation: 15
Actually, there seems to be something else I am misunderstanding along the way. I wrote getpid.sh as below:
Code:
#!/bin/sh

export RESULT=$(ps -A | grep $1 | awk '{print $1}')
I then execute the following lines:
Code:
$ getpid.sh emacs
$ echo $RESULT
Yet RESULT is still null. If I add an echo to the script, then it reports it correctly. How do I make the environment variable continue to live after the script has ended... or is the script the full extent of its environment and as such this is not possible?
 
Old 02-15-2007, 09:58 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
A separate instance of your shell is launched for the script. It cannot put variables back into the parent environment. If you want that behavior, try something like "source getpid.sh emacs" which causes the current shell to execute the getpid.sh script.

Also, have you looked at the 'pidof' utility?
 
Old 02-15-2007, 11:13 PM   #6
Elric of Grans
Member
 
Registered: May 2005
Posts: 46

Original Poster
Rep: Reputation: 15
Thanks for the help there. I was not familiar with `source', but it does the trick. I had not come across pidof before either, which does do exactly the same thing I had been doing through a longer process. I should have guessed there would be an easier way
 
Old 02-15-2007, 11:39 PM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Elric of Grans
Thanks for the help there. I was not familiar with `source', but it does the trick. I had not come across pidof before either, which does do exactly the same thing I had been doing through a longer process. I should have guessed there would be an easier way
It was a good learning process, wasn't it? I love reinventing utilities or making them work the way I want.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
Shell scripting: How to redirect output from within the script itself? Arodef Linux - General 4 05-23-2006 07:30 PM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM
output one page at a time in shell scripting sailu_mvn Linux - Software 2 01-06-2006 02:04 PM
script is capturing output.... wedgeworth Linux - Software 7 10-20-2003 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:24 AM.

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