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? |
var=$(ps -A | grep $program | awk '{print $1}')
|
Ahh, so you need to make it the result of the whole block rather than just the final program! Thanks for the help there :)
|
Actually, there seems to be something else I am misunderstanding along the way. I wrote getpid.sh as below:
Code:
#!/bin/sh Code:
$ getpid.sh emacs |
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? |
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 ;)
|
Quote:
|
All times are GMT -5. The time now is 03:00 AM. |