Operating System - HP-UX
1846306 Members
3586 Online
110256 Solutions
New Discussion

Re: Help need on perl scripting

 
SOLVED
Go to solution
manoj_pu
Regular Advisor

Help need on perl scripting

Hello

Can any tell me how to execute two unix commands by using perl "exec()" function.

I want to execute below two commands in one shot.
1st Command is
DISPLAY=`/usr/bin/hostname`:10.0; export DISPLAY

2nd command is
/usr/bin/X11/mwm -display :10.0

Thanks & Regards
Manoj
Leave with out tense and try best you gets result
6 REPLIES 6
Rodney Hills
Honored Contributor
Solution

Re: Help need on perl scripting

In perl it would be very similiar-

$display=`/usr/bin/hostname` . ":10.0";
system("DISPLAY='$display' ; /usr/bin/X11/mwm -display:10.0");

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Help need on perl scripting

This is not a valid use of the exec() function in Perl. Just like its C system call counterpart, exec() replaces the currently executing process and never returns so only the first exec() will ever be executed. You need to use the system() function instead which forks a child process to execute the command.
If it ain't broke, I can fix that.
Simon Hargrave
Honored Contributor

Re: Help need on perl scripting

If you mean you want to run them one after the other but in one call to exec, then just chain them (like you have already chained DISPLAY= and export in the 1st "command"):

exec 'DISPLAY=`/usr/bin/hostname`:10.0; export DISPLAY ; /usr/bin/X11/mwm -display :10.0'

If you want to run them independantly in parallel, then there's no point. All your 1st command does is export a variable. Why do this if you're not going to use it, it'll disappear when you exit the exec anyhow.
Simon Hargrave
Honored Contributor

Re: Help need on perl scripting

Of course Rodney and Clay are correct in that you should use system() not exec()!
manoj_pu
Regular Advisor

Re: Help need on perl scripting

Thanks Everyone for the quick help.


Warm regards,

Manoj
Leave with out tense and try best you gets result
H.Merijn Brand (procura
Honored Contributor

Re: Help need on perl scripting

And perl promotes the %ENV hash

use Sys::Hostname;
$ENV{DISPLAY}=hostname.":10.0";
system("/usr/bin/X11/mwm");

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn