Perl 脚本不执行某些外部调用
我编写了这个 Perl 脚本来自动化我的无线连接:
#!/usr/bin/perl
use strict;
my @modes = ("start", "stop");
my $mode = $modes[0];
my $kill_command = "sudo kill -TERM ";
sub check_args
{
if($#ARGV != 0)
{
print(STDERR "Wrong arguments\n");
print(STDERR "Usage: ./wicd.pl start|stop\n");
exit();
}
my @aux = grep(/^$ARGV[0]$/, @modes);
if (!@aux)
{
print(STDERR "Unknown argument\n");
print(STDERR "Usage: ./wicd.pl start|stop\n");
exit();
}
$mode = $ARGV[0];
}
check_args();
my @is_wicd_running = `ps -A | grep wicd`;
# START
if ($mode eq $modes[0])
{
if (!@is_wicd_running)
{
system("gksudo ifconfig wlan0 down");
system("sudo macchanger -r wlan0");
system("sudo wicd");
}
my @is_wicd_gui_running = grep(/wicd-client/, @is_wicd_running);
if (!@is_wicd_gui_running)
{
system("gksudo wicd-gtk &");
}
}
# STOP
else
{
for (@is_wicd_running)
{
my @aux = split(/ /, $_);
system("$kill_command$aux[1]");
}
system("sudo ifconfig wlan0 down");
}
问题是 macchanger 和 sudo ifconfig wlan0 down 没有执行(只有那些......)。奇怪的是,当通过 Perl 调试器 (perl -d) 调用脚本时,这些调用确实执行。我认为这可能是一个计时问题,并在这些调用之前添加了一些 sleep() 调用,但没有改变。我也尝试过使用 system() 调用,也没有进行任何更改。
编辑:更奇怪的是,我发现如果我将脚本作为 perl wicd.pl 运行,它会正常运行,而 ./wicd.pl 则不会(它运行但存在上述问题)。我已附上整个脚本。标头中使用的 Perl 解释器与 which perl 命令返回的相同。
有什么线索吗?提前致谢!
I have wrote this Perl script to automate my wireless connections:
#!/usr/bin/perl
use strict;
my @modes = ("start", "stop");
my $mode = $modes[0];
my $kill_command = "sudo kill -TERM ";
sub check_args
{
if($#ARGV != 0)
{
print(STDERR "Wrong arguments\n");
print(STDERR "Usage: ./wicd.pl start|stop\n");
exit();
}
my @aux = grep(/^$ARGV[0]$/, @modes);
if (!@aux)
{
print(STDERR "Unknown argument\n");
print(STDERR "Usage: ./wicd.pl start|stop\n");
exit();
}
$mode = $ARGV[0];
}
check_args();
my @is_wicd_running = `ps -A | grep wicd`;
# START
if ($mode eq $modes[0])
{
if (!@is_wicd_running)
{
system("gksudo ifconfig wlan0 down");
system("sudo macchanger -r wlan0");
system("sudo wicd");
}
my @is_wicd_gui_running = grep(/wicd-client/, @is_wicd_running);
if (!@is_wicd_gui_running)
{
system("gksudo wicd-gtk &");
}
}
# STOP
else
{
for (@is_wicd_running)
{
my @aux = split(/ /, $_);
system("$kill_command$aux[1]");
}
system("sudo ifconfig wlan0 down");
}
The problem is that macchanger and sudo ifconfig wlan0 down are not executing (only those...). The weird thing is that those call do execute when calling the script through Perl debugger (perl -d). I thought this could be a timing problem and added some sleep() calls before those calls, but no change. I also tried with system() calls with no change as well.
EDIT: more strange, I've found that if I run the script as perl wicd.pl it runs properly, while ./wicd.pl does not (it runs but has the problem described above). I've attached the whole script. The Perl interpreter used on the header is the same that which perl command returns.
Any clues? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更多信息可能会有所帮助,同时确保 \n 始终结束输出行。 今天早上尝试运行命令,
没有时间运行此代码,并且无法删除我之前的评论。但是运行“which”命令也可以确保您的命令位于 PATH 上。
More information may help, along with assuring that a \n always ends the output line. Try your running commands within
No time to run this code this morning and can't delete my prior comment. But somethings running a "which" command can also assure your command is on PATH.