Click here to get back home

executing source command from perl

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
executing source command from perl david 02-28-2008
Posted by david on February 28, 2008, 8:23 am
Please log in for more thread options
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

Posted by Peter Makholm on February 28, 2008, 8:28 am
Please log in for more thread options

> 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

Posted by david on February 28, 2008, 8:32 am
Please log in for more thread options
> > 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

Posted by Mario D'Alessio on February 28, 2008, 10:44 am
Please log in for more thread options

>> > 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



Posted by Ben Morrow on February 29, 2008, 2:49 pm
Please log in for more thread options

>
> >> > 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


Similar ThreadsPosted
Perl executing a command (on UNIX) August 13, 2007, 11:45 pm
Problem in Executing system command in a forking server September 22, 2005, 11:38 am
generating C source from Perl source December 5, 2005, 11:46 am
SSI executing Perl September 14, 2004, 4:56 pm
Executing cmd from perl script September 8, 2004, 9:49 pm
Executing awk from perl script March 24, 2007, 12:55 pm
executing an sql statement in perl May 21, 2007, 6:11 am
crontab for executing perl script August 3, 2005, 3:56 pm
executing perl script without .pl extension August 2, 2006, 10:24 am
Perl Module not executing on Windows 2000 May 2, 2005, 4:56 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap