executing source command from perl

Discussion in 'Perl Misc' started by david, Feb 28, 2008.

  1. david

    david Guest

    Dear All,

    I want to automize a process in perl.
    In this process I have the following code



    sub run_command {
    my ($command) = @_;
    print "$command\n";
    system($command) == 0
    or die "system $command failed: $? -- $!"

    }

    run_command('source init_file');
    run_command('mycommand.pl -configfile config -exec ');


    I get the following error
    source init
    Can't exec "source": No such file or directory at ..
    system source init failed: -1 -- No such file or directory at ...

    Can please someone help me to understand what i did wrong.

    Best regards,
    David
     
    david, Feb 28, 2008
    #1
    1. Advertising

  2. david <> writes:

    > run_command('source init_file');
    > run_command('mycommand.pl -configfile config -exec ');


    I'm guessing that you are trying to read some environment variables
    from init_file before running mycommand.pl?

    That won't work. First problem is that source is a shell builtin and
    not an independent program, this is why you are getting an
    error. Another problem is that child processes can't set environment
    variables if the parrent process and even if source was a real
    program, it couldn't set the environment for mycommand.pl

    //Makholm
     
    Peter Makholm, Feb 28, 2008
    #2
    1. Advertising

  3. david

    david Guest

    On Feb 28, 3:28 pm, Peter Makholm <> wrote:
    > david <> writes:
    > > run_command('source init_file');
    > > run_command('mycommand.pl -configfile config -exec ');

    >
    > I'm guessing that you are trying to read some environment variables
    > from init_file before running mycommand.pl?
    >
    > That won't work. First problem is that source is a shell builtin and
    > not an independent program, this is why you are getting an
    > error. Another problem is that child processes can't set environment
    > variables if the parrent process and even if source was a real
    > program, it couldn't set the environment for mycommand.pl
    >
    > //Makholm


    I understand, so it is better for this purpose to write a shell
    script .
    Thank you very much for your quick answer,
    David
     
    david, Feb 28, 2008
    #3
  4. david <> wrote:
    [...]
    > system($command) == 0
    > or die "system $command failed: $? -- $!"

    [...]
    >I get the following error
    >source init
    >Can't exec "source": No such file or directory at ..
    >system source init failed: -1 -- No such file or directory at ...
    >
    >Can please someone help me to understand what i did wrong.


    See third paragraph in the documentation for system():

    perldoc -f system

    jue
     
    Jürgen Exner, Feb 28, 2008
    #4
  5. "david" <> wrote in message
    news:...
    > On Feb 28, 3:28 pm, Peter Makholm <> wrote:
    >> david <> writes:
    >> > run_command('source init_file');
    >> > run_command('mycommand.pl -configfile config -exec ');

    >>
    >> I'm guessing that you are trying to read some environment variables
    >> from init_file before running mycommand.pl?
    >>
    >> That won't work. First problem is that source is a shell builtin and
    >> not an independent program, this is why you are getting an
    >> error. Another problem is that child processes can't set environment
    >> variables if the parrent process and even if source was a real
    >> program, it couldn't set the environment for mycommand.pl
    >>
    >> //Makholm

    >
    > I understand, so it is better for this purpose to write a shell
    > script .
    > Thank you very much for your quick answer,
    > David


    Not necessarily. You could do something like this:

    run_command(qq!sh -c "source init_file; mycommand.pl -configfile
    config -exec" !);

    It's been a LONG time since I worked in Unix, so my syntax may be incorrect.
    But hopefully it will help you get it to work. Basically, call the shell
    directly
    and have it source the init file and then run the command. In fact, in this
    case
    the Perl "system" command will probably be using the shell anyways, so
    you may not even need to call the shell explicitly:

    run_command(qq!source init_file; mycommand.pl -configfile config -exec!);

    Try it out and see if it works. Good luck.

    Mario
     
    Mario D'Alessio, Feb 28, 2008
    #5
  6. david

    Ben Morrow Guest

    Quoth "Mario D'Alessio" <>:
    >
    > "david" <> wrote in message
    > news:...
    > > On Feb 28, 3:28 pm, Peter Makholm <> wrote:
    > >> david <> writes:
    > >> > run_command('source init_file');
    > >> > run_command('mycommand.pl -configfile config -exec ');
    > >>
    > >> I'm guessing that you are trying to read some environment variables
    > >> from init_file before running mycommand.pl?
    > >>
    > >> That won't work. First problem is that source is a shell builtin and
    > >> not an independent program, this is why you are getting an
    > >> error. Another problem is that child processes can't set environment
    > >> variables if the parrent process and even if source was a real
    > >> program, it couldn't set the environment for mycommand.pl

    > >
    > > I understand, so it is better for this purpose to write a shell
    > > script .
    > > Thank you very much for your quick answer,

    >
    > Not necessarily. You could do something like this:
    >
    > run_command(qq!sh -c "source init_file; mycommand.pl -configfile
    > config -exec" !);


    This is somewhat pointless. Simply using perl to run sh for you is
    evidence that sh would have been a better tool in the first place :).

    The module Shell::GetEnv will allow you to extract the environment from
    a subshell:

    use Shell::GetEnv;

    Shell::GetEnv->new(sh => 'source init_file')->import_envs;
    run_command 'mycommand.pl...';

    Also, the IPC::Run module is a much more convenient and reliable way of
    running external commands that rolling your own using system.

    Ben
     
    Ben Morrow, Feb 29, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun
    Replies:
    4
    Views:
    338
    anonymous
    Jan 3, 2005
  2. Nick
    Replies:
    14
    Views:
    352
    Tad McClellan
    Oct 3, 2003
  3. sunil
    Replies:
    2
    Views:
    160
    Tintin
    Dec 25, 2003
  4. Replies:
    2
    Views:
    168
    Tad McClellan
    Mar 26, 2005
  5. Kuhl
    Replies:
    1
    Views:
    297
    Tad J McClellan
    Dec 22, 2008
Loading...

Share This Page