Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Automating a shell session: cd does not work

by pokki (Monk)
on Jan 02, 2013 at 15:03 UTC ( #1011285=perlquestion: print w/replies, xml ) Need Help??
pokki has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks,

I'm in the process of writing a module to test shell sessions using IPC::Run, and I've hit the hurdle described in the subject line: I can't manage to make cd work.

I tried various things until I reread the IPC::Run documentation which states "No support for cd, setenv, or export: do these in an init() sub (example follows)". Okay, but I don't just need cd once to put my script in the right directory, I need a fully interactive session that accepts any command. (Does anyone know why this limitation exists, by the way?)

If you're curious, this is the kind of tool you feed an almost-but-not-quite shell script with the expected output after every command. Then the tool runs every command and compares the output. This is highly useful for teaching material when you need to demonstrate how, e.g., a DVCS works (if you've used Cram before, the example will look rather familiar):

#!/bin/bash # Do all the work in /tmp $ cd /tmp # Testing presence of Mercurial $ hg --version re: Mercurial Distributed SCM \(version [\d.]+\) (see for more information) re: Copyright \(C\) [\d-]+ Matt Mackall and others This is free software; see the source for copying conditions. Ther +e is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. # Creating a repository $ mkdir foo $ cd foo $ pwd /tmp/foo $ hg init $ echo "this is a file" > content $ cat content this is a file $ hg add content $ hg st A content $ hg commit -m "created repo and added a file" # Checking that everything looks good $ hg log re: changeset: 0:[a-f0-9]{12} tag: tip user: Fabrice Gabolde <> re: date: .* summary: created repo and added a file

Lines starting with $ are shell commands, the others are expected output. There are facilities for globs/regexes, comments, etc (not everything demonstrated here). The file above gives me:

# cd /tmp # hg --version ok 1 - regex match of 'Mercurial Distributed SCM \(version [\d.]+\)' ok 2 - literal match of '(see for more in +formation)' ok 3 - literal match of '' ok 4 - regex match of 'Copyright \(C\) [\d-]+ Matt Mackall and others' ok 5 - literal match of 'This is free software; see the source for cop +ying conditions. There is NO' ok 6 - literal match of 'warranty; not even for MERCHANTABILITY or FIT +NESS FOR A PARTICULAR PURPOSE.' # mkdir foo # cd foo # pwd not ok 7 - literal match of '/tmp/foo' # Failed test 'literal match of '/tmp/foo'' # at lib/ line 87. # got: '/home/fgabolde/work/Lembas' # expected: '/tmp/foo' # hg init # echo "this is a file" > content # cat content # STDERR: cat: content # STDERR: : No such file or directory

... and it all goes downhill from there, because there are other issues with the code. (The full script is there just to show people what I'm working on and hopefully garner criticism as a side-effect; in reality I have a test on pwd right after cd /tmp and it fails as well.) The foo directory doesn't seem to be created anywhere (says find / -iname foo) either, but cd or mkdir don't complain at all (like cat does at the end)

I'm using IPC::Run basically like this (shortened):

my ($input, $output, $errput); my $subprocess = start([ '/bin/bash' ], '<', \$input, '1>pty>', \$output, '2>', \$errput); # later, for each $command { shell => 'pwd', 'outputs' => [ '/tmp/foo' + ] } diag $command->{shell}; $input = $command->{shell} . "\n"; foreach my $expected_output (@{$command->{outputs}}) { while ($output !~ /\n/) { if ($errput) { diag 'STDERR: '.$errput; $errput = ''; } $subprocess->pump; } # munge $expected_output and compare stuff }

Does anyone automate shell(or other CLI apps) sessions to this degree? With what module? Looking around on CPAN it seems only IPC::Run really does the whole "talk to a subprocess double-pipe style" hog. I don't get why bash seems to "reset" between every command, though. Did I miss something really obvious?

Replies are listed 'Best First'.
Re: Automating a shell session: cd does not work
by Illuminatus (Curate) on Jan 02, 2013 at 22:31 UTC
    Did I miss something really obvious?

    The following code shows a successful 'cd' for me

    #! /usr/bin/perl use strict; use IPC::Run qw( start pump finish ); my ($input, $output, $errput); $input=""; $output=""; my $subp = start([ '/bin/bash' ], \$input, \$output, \$errput) or die $@; print $errput; $input .= "cd /work/drjohnson\n"; pump $subp until length $input == 0; $input .= "pwd\n"; pump $subp until $output =~ /\n/; print "$output\n"; $subp->finish;
    If you have a runnable example of where it doesn't work for you, I'd be glad to try it. You might also try IPCRUNDEBUG. Do an 'export IPCRUNDEBUG=details' before running your script. I believe what the author meant was that you couldn't do things like 'cd' (well, chdir) in your parent script between pump calls.


      Yes, your example works for me too. I'll take a closer look at my code, there's probably a glaring mistake somewhere in my loop. I'll sleep over it and see if my brain can spot it better tomorrow :)

      In any case this will probably end on Bitbucket soonish for the world to poke at it and point more bugs, so if I haven't found anything tomorrow I'll take you up on your offer and post the repo URL here.

      Thanks for your time and hints so far.

      OK, it was definitely my fault. I keep telling myself I need to stop assuming the modules I use are at fault.

      I was doing $input = "new command\n" instead of $input .= "new command\n", so I guess in some cases existing input got clobbered before being pumped into the harness.

      Thanks again!

Re: Automating a shell session: cd does not work
by Anonymous Monk on Jan 02, 2013 at 20:23 UTC

    I think it is because IPC::Run is not Expect

    You can always use chdir

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011285]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2018-06-19 07:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.