Home > Archive > PERL Miscellaneous > February 2008 > executing source command from perl
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
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 2008-02-28, 8:07 am |
| 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
| |
|
| On Feb 28, 3:28 pm, Peter Makholm <pe...@makholm.net> wrote:
> david <michaelg...@gmail.com> writes:
>
> 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 2008-02-28, 8:07 am |
| 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 2008-02-29, 4:26 am |
|
"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:
>
> 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 2008-02-29, 7:10 pm |
|
Quoth "Mario D'Alessio" <dalessio@motorola.NOSPAM.com>:
>
> "david" <michaelgang@gmail.com> wrote in message
> news:5d735027-6b9e-4b2f-80d5-f77d1ffcbee4@i12g2000prf.googlegroups.com...
>
> 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
|
|
|
|
|