in reply to shell within system() call

As system always starts a new shell (or no shell at all, in case the command doesn't contain any shell metacharacters), the only way to make shell functions available is by sourcing the definitions in that new shell.  And in case you need a ksh, you'll also want to start the shell yourself:

system('/bin/ksh', '-c', '. /env.ksh ; shellFunction');

Or, in case env.ksh is huge, and you want to avoid re-sourcing it every time, you could open a pipe to the shell, e.g.

my $pid = open my $shell, "| /bin/ksh" or die "Couldn't start ksh: $!" +; print $shell ". /env.ksh\n"; print $shell "shellFunction\n"; # later print $shell "shellFunction2\n"; # ...

Replies are listed 'Best First'.
Re^2: shell within system() call
by shmem (Chancellor) on Mar 19, 2010 at 20:51 UTC

    All correct, but for the record, and since the sourced file is env.ksh - smells like setting up the environment? - one could also, if so inclined, stuff functions into environment variables, since those are propagated:

    qwurx [shmem] ~ > env BLORF='blorf () { echo $* blorf. };' sh -c 'IFS=" "; eval $BLORF; BLORF=""; blorf I say' I say blorf.

    ;-)

Re^2: shell within system() call
by claudius (Initiate) on Mar 22, 2010 at 20:55 UTC

    Hi again

    Many thanks to all that posted! Almut's answer was exactly what I needed: the env.ksh is indeed big enough such as to not want to source multiple times, and piping to the open shell child process is just the right thing to do for me.

    This is also because the first shellFunction invoked is an initialization required by all subsequent shellFunctions being called.

    Cheers,

    claudius