Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do the system() and backtick work?

Status
Not open for further replies.

BStopp

Programmer
Dec 29, 2003
29
US
After a few days working with what the system is doing here's what i came up with and my problem.

I execute a perl command via unix ".forward" file for incoming email. Piping the email contents to the script.
When the script executes it runs in a shell as the UID who is reciving the emails. However the actual environment for that user doesn't exist. (IE: No environment variables are available).

I need the environment available for the process to successfully complete it's task, so i tried to envoke the environment by doing both a system("/usr/local/me/.profile") command. This didn't work because it appears that when you envoke the Perl System command you create a child shell to run the process in and when it's done, the shell is gone.

So next i tried to envoke the environment with the backticks. `. /usr/local/me/.profile`
This didn't work either but i'm not sure why... Does it create a child shell as well?

Is it possible to envoke all the environment settings in the ".profile" file and have them availble to the current process? Or am i trying something that is impossible with Perl?

Thanks for any help.

B. Stopp
 
Yes, both of these create a child shell to run the command. You can not update the perl env from an externally excuted file.

What you can do though is excute the file and grab the output as a scalar and then parse that scalar. The command you call could just print the data to STDOUT, perl can grab it and then set its env internally.

 
What you might think of doing is reading the .profile, setting entries in the %ENV hash based on what's in the .profile and then you'll have that user's environment in your Perl script ready to run external programs with everything set correctly.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Thanks for your help, both of you. I was afraid that was the answer. Doing all of that work is really not what i had intended for the script. It was supposed to be very simple and very short. Doing the first suggested solution would be alot of processing. As for the second solution, that would work if there weren't multiple profiles (ie: .my_profile & .my_post_profile..) that the actual .profile envoked as it was processed (ie: .my_profile & .my_post_profile..).

I found an acceptable solution but i do appreciate the knowledge!

B. Stopp
 
i probably don't grok this, but something like su -l -c "perl full_path_to_myscript" user
might set up the environment. perl full_path_to_myscript would have to be rx by user.
There might be a problem might passing the mail file. Perhaps su -l -c "perl myscript full_path_mailfile" user.
full_path_mailfile would have to be readable by user.

just a thought



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top