Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   executing source command from perl (http://www.velocityreviews.com/forums/t906584-executing-source-command-from-perl.html)

david 02-28-2008 01:23 PM

executing source command from perl
 
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

Peter Makholm 02-28-2008 01:28 PM

Re: executing source command from perl
 
david <michaelgang@gmail.com> 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

david 02-28-2008 01:32 PM

Re: executing source command from perl
 
On Feb 28, 3:28 pm, Peter Makholm <pe...@makholm.net> wrote:
> david <michaelg...@gmail.com> 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

Jürgen Exner 02-28-2008 01:38 PM

Re: executing source command from perl
 
david <michaelgang@gmail.com> 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

Mario D'Alessio 02-28-2008 03:44 PM

Re: executing source command from perl
 

"david" <michaelgang@gmail.com> wrote in message
news:5d735027-6b9e-4b2f-80d5-f77d1ffcbee4@i12g2000prf.googlegroups.com...
> On Feb 28, 3:28 pm, Peter Makholm <pe...@makholm.net> wrote:
>> david <michaelg...@gmail.com> 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



Ben Morrow 02-29-2008 07:49 PM

Re: executing source command from perl
 

Quoth "Mario D'Alessio" <dalessio@motorola.NOSPAM.com>:
>
> "david" <michaelgang@gmail.com> wrote in message
> news:5d735027-6b9e-4b2f-80d5-f77d1ffcbee4@i12g2000prf.googlegroups.com...
> > On Feb 28, 3:28 pm, Peter Makholm <pe...@makholm.net> wrote:
> >> david <michaelg...@gmail.com> 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



All times are GMT. The time now is 05:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.