简体   繁体   English

通过PerlScript执行多个Unix命令

[英]Execute multiple unix command by perlscript

I am very new to perl. 我是Perl的新手。 I am trying to create a perl script which will execute multiple unix command to create VNC session in some unix server. 我正在尝试创建一个Perl脚本,该脚本将执行多个unix命令以在某些unix服务器中创建VNC会话。

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: 这使得Perl脚本基本上无关紧要,因为您可以在shell中将其编写为:

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

It might be better to use sudo rather than su . 使用sudo而不是su可能更好。 The difference is that with su , you have to know the other user's password; 区别在于使用su ,您必须知道其他用户的密码; 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). 使用sudo ,您只需要知道自己的密码(系统管理员必须已授予您使用sudo进行手头任务的权限)。

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. 因此,在这种情况下,我想像su永远不会退出,并且vncserver不会与您想要的用户一起运行(也不会退出以将控制权返回到perl脚本)-因为它是在另一个su从未发生过的子shell中执行的。

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. 尽管这是否行得通,但您必须自己找出答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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