Re: Change shell within Perl
by blazar (Canon) on Sep 13, 2005 at 14:15 UTC
|
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?
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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...
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
What do you mean by change? Do you want a new window?
| [reply] |
Re: Change shell within Perl
by puploki (Hermit) on Sep 13, 2005 at 13:58 UTC
|
I'm not sure in what context you're doing this, so this might not be the answer you're after, but you can use fork() to start an external process and have Perl continue without waiting for that process to finish and return.
Update:To answer your second question which I didn't spot the first time around, your current shell should be set in the environment variable $ENV{SHELL}
Update 2:Brain not working today, backticks *doesn't* fork into the background. God knows why I though it did! Thank you fishbot_v2 for reminding me :)
| [reply] |
Re: Change shell within Perl
by jch341277 (Sexton) on Sep 13, 2005 at 17:34 UTC
|
If you're looking to script your interaction with the shell then you can use Expect.
However, I'd be asking myself if all I'm doing with perl is running a shell script - why not just write a shell script?
| [reply] |
Re: Change shell within Perl
by zentara (Cardinal) on Sep 14, 2005 at 10:04 UTC
|
You could also try IPC::Open3 to get some other shell to run.
my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/tcsh');
.....
print IN "$mycommand\n";
my $results = <OUT>;
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |