http://www.perlmonks.org?node_id=18199


in reply to RE: RE: chdir vs. shell behavior
in thread chdir vs. shell behavior

On the one occasion I had cause to do this, I exec'd the perl script from the login script, and exec'd a new shell from the perl script. Problem was, that launched the shell twice - easily solved in my case, as I had the luxury of making the perl script the login shell for the relevant users. This also solves the problem of needing to stop the script from executing twice (either by assuming a login shell the first time around, or setting an environment variable from within the script)

As someone else pointed out, passing the output from the script to a cd command works too. But things complicate if you want to change more of the environment than just the directory...

That is (shell script):

# ... stuff run every time this script is invoked if [ "$checkvar" != "foo" ]; then exec perl /usr/script/myscript.pl else # ... stuff to do after the script has run fi
And in the perl script:
$ENV{checkvar}="foo"; exec "sh", "/usr/script/myshell.sh" # or, if you're logging in exec "sh", "-l"
There may be a problem if the shell script is not being executed as part of a login sequence, as you can't assume the environment is trustworthy. I suspect in most cases all someone who fakes the variable is going to do is make the perl script fail to run...