Debian Codegraph @

eg/runsh

raw
1 #!/usr/bin/perl -w
2
3 ## Demonstration of chatting with a bash shell.
4
5 use strict;
6
7 use IPC::Run qw( start pump finish timeout );
8
9 my ( $in, $out, $err );
10
11 my $h = start(
12 [qw(sh -login -i )], \$in, \$out, \$err,
13 debug => 0,
14 timeout(5),
15 );
16
17 ## The first thing we do is to convert the user's prompt. Normally, we would
18 ## do a '' as the first command in the for () loop so we could detect errors
19 ## that bash might emit on startup. In this case, we need to do this
20 ## initialization first so that we have a prompt to look for so we know that
21 ## it's ready to accept input. This is all because the startup scripts
22 ## that bash runs set PS1, and we can't have that.
23 $in = "PS1='<PROMPT> '\n";
24
25 ## bash prompts on stderr. Consume everything before the first
26 ## <PROMPT> (which is the second prompt bash issues).
27 pump $h until $err =~ s/.*(?=^<PROMPT> (?!\n)\Z)//ms;
28
29 for (qw( ls ps fOoBaR pwd )) {
30 $in = $_ . "\n";
31 $out = '';
32 pump $h until $err =~ s/\A(<PROMPT> .*)(?=^<PROMPT> (?!\n)\Z)//ms;
33 print map { "sh err: $_\n" } split( /\n/m, $1 );
34 print map { "sh: $_\n" } split( /\n/m, $out );
35 }
36
37 finish $h ;