in reply to Change shell within Perl

I was wondering if anyone here has some bright ideas on how to (tempurary) change a shell from within a Perl script without the script actually "end"! I mean, say I do "system ("bash");", the script won't continue until I do "exit" in that newly opened bash shell. Is it even possible?
Indeed this is what system should do in the first place. Did you try it? Or do you want to do something different?
[blazar@zion blazar]$ perl -le 'print 1; system "bash"; print 2' 1 [blazar@zion blazar]$ whoami blazar [blazar@zion blazar]$ exit 2 [blazar@zion blazar]$
Ah, yea, and how to figure out _what_ shell I'm currently in?
Well, it depends on what you mean exactly with "shell I'm currently in". If the perl script is launched from a shell, and thus is a child of it, then it can check the cmdline of the parent process. Is this what that you want?

Replies are listed 'Best First'.
Re^2: Change shell within Perl
by Ace128 (Hermit) on Sep 13, 2005 at 15:12 UTC
    Ok, the question here was how to _NOT_ having to enter exit to continue? Like I want something like: do stuff; change to another shell; do stuff; change back (or enter yet another shell)...
    And not type exit... Seems like its abit tricky... hmm...

    Well, yes, the $ENV{SHELL} helped me there.
      It all depends on "do stuff". Basically it seems you do want to start an interactive shell. Don't you? Then you have to tell it you're finished in some way or another, to return to the main flow of your Perl program. So this won't be a 'real' answer to your question, but if you don't want to type exit, alternatives are logout or ^D (at the beginning of a line).

      Or else "do stuff" is a single command in the shell you're opening. But then why not calling that command directly? All in all I suspect XY here: you really want to do "Y", but think you need "X" to do "Y", thus you ask "X" whereas you should ask "Y" in the first place...

        Something like:
        system ("tcsh");
        system ("java -jar gogo.jar");
        system ("bash");
        system ("mld gogo");
      What do you mean by change? Do you want a new window?