Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > executing source command from perl

Reply
Thread Tools

executing source command from perl

 
 
david
Guest
Posts: n/a
 
      02-28-2008
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
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      02-28-2008
david <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
david
Guest
Posts: n/a
 
      02-28-2008
On Feb 28, 3:28 pm, Peter Makholm <(E-Mail Removed)> wrote:
> david <(E-Mail Removed)> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-28-2008
david <(E-Mail Removed)> 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
 
Reply With Quote
 
Mario D'Alessio
Guest
Posts: n/a
 
      02-28-2008

"david" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 28, 3:28 pm, Peter Makholm <(E-Mail Removed)> wrote:
>> david <(E-Mail Removed)> 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


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-29-2008

Quoth "Mario D'Alessio" <(E-Mail Removed)>:
>
> "david" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Feb 28, 3:28 pm, Peter Makholm <(E-Mail Removed)> wrote:
> >> david <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why in perl system(" ") command, "source command_file" does nothing,but "source command_file && echo" works? Kuhl Perl Misc 1 12-22-2008 02:29 PM
Pull out only first field of ps command ( in Perl ) PERL RUN COMMAND tweetiebirds@gmail.com Perl Misc 2 03-26-2005 06:03 AM
Executing a string related to source code, not the command line Arun Java 4 01-03-2005 03:26 PM
Problem with executing UNIX command in perl script sunil Perl Misc 2 12-25-2003 04:03 AM
Perl CGI executing command line functions Nick Perl Misc 14 10-03-2003 03:05 PM



Advertisments