LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell scripting, capturing the output... (https://www.linuxquestions.org/questions/linux-general-1/shell-scripting-capturing-the-output-529455/)

Elric of Grans 02-15-2007 06:36 PM

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?

fudam 02-15-2007 07:13 PM

var=$(ps -A | grep $program | awk '{print $1}')

Elric of Grans 02-15-2007 07:23 PM

Ahh, so you need to make it the result of the whole block rather than just the final program! Thanks for the help there :)

Elric of Grans 02-15-2007 07:48 PM

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?

Matir 02-15-2007 09:58 PM

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?

Elric of Grans 02-15-2007 11:13 PM

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 ;)

Matir 02-15-2007 11:39 PM

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.


All times are GMT -5. The time now is 03:00 AM.