Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.