Small doubt in perl

A

anil

Hi,
I need a small help, how shall i call shell script and java
program in the perl?

I am using the following snippet of code,

system("sample.sh");
system("java test");

In the "sample.sh" i have set some environmental variables, but the
environment variables which are set in the sample.sh are not used by
the java pgm.

Is there any other way to achieve this functionality?

Thanks & Regards
Anil.
 
J

John W. Krahn

anil said:
Hi,
I need a small help, how shall i call shell script and java
program in the perl?

I am using the following snippet of code,

system("sample.sh");
system("java test");

Open two xterms (or your favourite terminal program) and run "sample.sh" in
one and "java test" in the other. This is essentially what you are doing there.
In the "sample.sh" i have set some environmental variables, but the
environment variables which are set in the sample.sh are not used by
the java pgm.

How do you set them?
Is there any other way to achieve this functionality?

open SHELL, '>>', 'sample.sh' or die "Cannot open 'sample.sh' $!";
print SHELL "java test\n";
close SHELL;
system( '/bin/sh', 'sample.sh' ) == 0 or die "system '/bin/sh sample.sh'
failed: $?";




John
 
D

Dave Weaver

system("sample.sh");
system("java test");

In the "sample.sh" i have set some environmental variables, but the
environment variables which are set in the sample.sh are not used by
the java pgm.

perldoc -q environ
 
M

Michele Dondi

Subject: Small doubt in perl
http://perlmonks.org/?node_id=444996

system("sample.sh");
system("java test");

In the "sample.sh" i have set some environmental variables, but the
environment variables which are set in the sample.sh are not used by
the java pgm.

Is there any other way to achieve this functionality?

system qw/sh -c/ => 'sample.sh; java test';


Michele
 
A

anil

Open two xterms (or your favourite terminal program) and run "sample.sh" in
one and "java test" in the other. This is essentially what you are doing there.


How do you set them?


open SHELL, '>>', 'sample.sh' or die "Cannot open 'sample.sh' $!";
print SHELL "java test\n";
close SHELL;
system( '/bin/sh', 'sample.sh' ) == 0 or die "system '/bin/sh sample.sh'
failed: $?";

John

Thank You very much for ur valuable replies......
 
A

anil

Open two xterms (or your favourite terminal program) and run "sample.sh" in
one and "java test" in the other. This is essentially what you are doing there.


How do you set them?


open SHELL, '>>', 'sample.sh' or die "Cannot open 'sample.sh' $!";
print SHELL "java test\n";
close SHELL;
system( '/bin/sh', 'sample.sh' ) == 0 or die "system '/bin/sh sample.sh'
failed: $?";

John

Hi john,

Thanks for your reply, but your method is corrupting the
sample.sh, I don't want to corrupt the sample.sh, because this perl
program i have to execute several times, then that java program runs
several times..which degrades the performance of my program.

Thanks
Anil.
 
B

Ben Morrow

Quoth Michele Dondi said:
system qw/sh -c/ => 'sample.sh; java test';

This won't work. sample.sh is still run as a subprocess of the shell.
You need to locate the shell used for sample.sh (I'll assume /bin/sh),
and run

system '/bin/sh', -c => '. sample.sh; java test';

Note the '.', which makes the shell run the script in the same process.
It may be better to use

system '/bin/sh', -c => '. sample.sh; exec java test';

which will save a fork.

Ben
 
M

Michele Dondi

This won't work. sample.sh is still run as a subprocess of the shell.
You need to locate the shell used for sample.sh (I'll assume /bin/sh),
and run

system '/bin/sh', -c => '. sample.sh; java test';

Note the '.', which makes the shell run the script in the same process.

D'Oh! It's true...


Michele
 
A

anil

This won't work. sample.sh is still run as a subprocess of the shell.
You need to locate the shell used for sample.sh (I'll assume /bin/sh),
and run

system '/bin/sh', -c => '. sample.sh; java test';

Note the '.', which makes the shell run the script in the same process.
It may be better to use

system '/bin/sh', -c => '. sample.sh; exec java test';

which will save a fork.

Ben

For curiosity, Is there any method to run on windows also, suppose we
have sample.bat instead of sample.sh ?
 
M

Michele Dondi

For curiosity, Is there any method to run on windows also, suppose we
have sample.bat instead of sample.sh ?

Exactly the same except that I don't know whether Win's scripting
language allow for the equivalent of sourcing. If it's just a matter
of setting some environment variables one may avoid a separate shell
script and massage %ENV directly from perl, though.


Michele
 
A

anil

For curiosity, Is there any method to run on windows also, suppose we
have sample.bat instead of sample.sh ?

Exactly the same except that I don't know whether Win's scripting
language allow for the equivalent of sourcing. If it's just a matter
of setting some environment variables one may avoid a separate shell
script and massage %ENV directly from perl, though.

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

I have searched in the net and i found the solution we have to
replace ; with & in the above system command. Thanks for all your
valuable replies...
 
M

Michele Dondi

^
^

[snip]
I have searched in the net and i found the solution we have to
replace ; with & in the above system command. Thanks for all your
valuable replies...

Please note that it probably won't be enough, since I don't expect

.. FILE

to valid syntax there. But of course I may be wrong, and that would
certainly be good for you.


Michele
 
B

Ben Morrow

Quoth Michele Dondi said:
system '/bin/sh', -c => '. sample.sh; exec java test';
^
^

[snip]
I have searched in the net and i found the solution we have to
replace ; with & in the above system command. Thanks for all your
valuable replies...

Please note that it probably won't be enough, since I don't expect

. FILE

to valid syntax there. But of course I may be wrong, and that would
certainly be good for you.

No, it's not. However, cmd.exe *always* sources .bat files (yes,
cringe... it's the legacy of DOS, which Didn't Do Processes), so

system cmd => '/c' => 'sample.bat& java test';

should work (untested, and possibly

system 'cmd /c "sample.bat& java test"';

may work better).

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

Forum statistics

Threads
474,269
Messages
2,571,100
Members
48,773
Latest member
Kaybee

Latest Threads

Top