in reply to chdir vs. shell behavior

I'd really love to understand how to do this as well. The supplied answers suggest .profile modifications but this only solves login initialization before the "real" shell is invoked (right?). I've been longing to port lots of my old 4Dos batch files to Perl but they won't affect my current shell environment. I don't want to write shell scripts. I'm just not a "fi" kinda guy. I have an script "mc" which takes one param and does a mkdir && a chdir on it which I find very useful... in 4Dos but Perl can't do it. Even if it's convoluted or compromises security or whatever, doesn't anybody know how to manipulate your parent process effectively from a Perl script? If I ever figure out on my own, I'll be sure to post it but all I've ever heard is it can't be done and I haven't had time to try to be underhanded myself yet. Hopefully somebody knows. TTFN & Shalom.

-PipTigger

p.s. Byslexia is a Ditch!

Replies are listed 'Best First'.
RE: RE: chdir vs. shell behavior
by ahunter (Monk) on Jun 15, 2000 at 21:47 UTC
    It is a FAQ anyway, but I'll explain and risk setting a precedent...

    DOS does not have anything remotely like the Unix concept of a process. When a piece of software in DOS launches another one, it loads it into its own memory space and continues from that programs start point. This means that all programs under DOS share the same environment, including directory, environment variables, and so on.

    In Unix and workalikes, there is the concept of processes. These have their own environment, and usually have a parent process, which their initial environment is inherited from. This means that when you start a new process with system() or whatever, it gets a copy of CSD and environment variables of the process that started it. When it alters its environment, it only affects itself and any child processes it launches after that point. So, if you do a chdir from a process, it only affects that process's environment. If you consider that under Unix that the processes could be running in parallel, you should realise that this is a Good Thing.

    However, this means that a child process cannot directly manipulate its parents environment. It needs to communicate with the parent process - easiest way being backticks, supported in both perl and shell scripts. IE:

    eval `myscript.pl`
    Would cause the output of myscript to be evaluated in the process that launched it.

    Next up - the meaning of exec()! exec() is the closest thing Unix has to DOS's model of execution - it replaces the program running in the current process with a new one, without creating a new process. The old program is never re-entered, though - if you want to do that, you have to do it yourself (the design of most modern Unixen makes this less costly than most people think). A lot of beginners make the mistake of using system() where exec() would be more appropriate (system() does a fork() and then an exec() under Unix. Actually, it also waits for the child process to terminate). Use exec() if you've finished with the current process.

    Andrew (mysteries explained, or at least made more mysterious)