Re: Calling shell-specific commands
- From: jen.spinney@xxxxxxxxx
- Date: 20 Feb 2007 06:28:46 -0800
On Feb 19, 4:04 pm, "Paul Lalli" <mri...@xxxxxxxxx> wrote:
On Feb 19, 12:10 pm, jen.spin...@xxxxxxxxx wrote:
Hello list!
This may be a bit of a shell/perl hybrid question. I have a perl
script that modifies my ~/.login and ~/.cshrc files (My shell is
tcsh). At the end of my script, I want to tell the shell to re-read
those files. I first tried the following snippet:
system 'source .login';
system 'source .cshrc';
I quickly found out that this command is simple enough that perl will
bypass the shell and execute the command directly. Of course, this is
exactly what I don't want, as 'source' means nothing outside the
context of a shell.
After hunting around on google a bit, I then tried the following
snippet:
system 'tcsh', '-c', 'source .login';
system 'tcsh', '-c', 'source .cshrc';
The first system call works fine; the .login file is clearly re-read
when I run my script. However, the second system call doesn't work.
That is, the .cshrc file is definitely not re-read. I've checked the
return value from system -- it's always 0. I've checked $!, and it's
not set.
Then I tried running the commands directly from within the shell ui
myself (where '>' is the prompt):
source .login # Works, .login is re-read; no output to STDERR or STDOUT
tcsh -c 'source .login' # Works, same as above
source .cshrc # Works, same as above
tcsh -c 'source .cshrc' # Doesn't work: .cshrc is not re-read; no output to STDERR or STDOUT
The above leads me to believe I have more of a shell-related problem
than a perl one... but I know there are so many tools in perl to
execute external commands. I've looked into the Shell module a bit,
but it doesn't have subroutines for shell-specific commands (like
source).
Any suggestions?
Your problem has nothing to do with shells. It has to do with the
entire logic behind what you're trying to do. When you execute an
external program, Perl creates a brand new process. That process's
environment is the one that gets modified. Then THAT PROCESS ENDS. A
child process CANNOT modify its parent's environment. This has
nothing to do with Perl. This is how Unix works.
See also: perldoc -q environment
If you want to modify the environment in which your Perl script is
executing, you have to modify the environment directly, via the %ENV
hash. Read and parse the files that contain the commands, and change
your %ENV appropriately.
Of course, even after you do that, once your Perl program ends, the
environmental changes will be lost. That's what's supposed to happen.
You need to rethink what it is you're trying to do.
Paul Lalli
Thanks Paul. It seems, then, that I'm at a loss for how to approach
this problem. I'm taking two courses that use the same linux system.
Both courses require me to use the course-specific .cshrc and .login
scripts. The scripts are incompatible between the different courses
(eg, they both redefine my JAVA_HOME and CLASSPATH to different
locations). So, I decided to save the scripts with their appropriate
course names and turn .cshrc and .login into symbolic links. I then
wrote a perl script that would (1) unlink the symbolic links and (2)
relink them to the appropriate scripts for the course I would specify
on the command line. (I considered bypassing the shell scripts
completely and modifying the environment variables from my own perl
script, but the shell scripts are very long and I thought that would
just be re-inventing the wheel). So, after I ran this unlinking/
relinking script, I would have to log out and then log back in (or
manually type "source .login" and "source .cshrc") to have the changes
take effect. What I was trying to do here was automate the changes-
taking-effect step. But maybe I'll just have to that manually? Or
maybe I should re-write my perl script as a shell script? Any ideas?
Thanks,
Jen
.
- Follow-Ups:
- Re: Calling shell-specific commands
- From: Paul Lalli
- Re: Calling shell-specific commands
- References:
- Calling shell-specific commands
- From: jen . spinney
- Re: Calling shell-specific commands
- From: Paul Lalli
- Calling shell-specific commands
- Prev by Date: Re: match string with slashes
- Next by Date: Re: Is there any way to use C header files in perl program?
- Previous by thread: Re: Calling shell-specific commands
- Next by thread: Re: Calling shell-specific commands
- Index(es):
Relevant Pages
|
|