简体   繁体   中英

Execute multiple unix command by perlscript

I am very new to perl. I am trying to create a perl script which will execute multiple unix command to create VNC session in some unix server.

Here is my script -

#!/usr/bin/perl -w
use Carp;
use strict;
use warnings;

# here get the parameters idsid
my $IdsId=$ARGV[0];


#excecute the commands here

my $user=`su -l $IdsId`;
my @finalresult=`vncserver -randr      1024x768,800x600,1024x768,1152x824,1280x1024,1280x800,1440x900,1400x1050,1600x1200,1920x1200`;

print "@finalresult";

But when I am executing this script its not working.

Please some body help me.

I would expect to see you executing:

exec "su", "-l", "$IdsID", "-c", "vncserver -randr ...";

which makes the Perl script largely irrelevant since you could write it in shell as:

exec su -l "${1:-$USER}" -c "vncserver -randr ..."

It might be better to use sudo rather than su . The difference is that with su , you have to know the other user's password; with sudo , you only have to know your own password (and the system administrator must have given you permission to use sudo for the task on hand).

When you use backticks, a sub shell is created and the main program is halted. When the process in that sub shell is finished, the control returns to the main program. So in this case, I imagine that su never exits, and that vncserver does not run with the user you intend (nor does it exit to return control to the perl script) -- because it is executed in another sub shell where su never happened.

What you probably need is to do these commands in the same line:

my @result = qx(su -l $IdsId; vncserver -randr ....);

Although whether this works or not you'll have to find out for yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM