executing source command from perl

D

david

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
 
P

Peter Makholm

david said:
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
 
D

david

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

Jürgen Exner

david said:
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
 
M

Mario D'Alessio

david said:
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
 
B

Ben Morrow

Quoth "Mario D'Alessio said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top