Perl 脚本不执行某些外部调用

发布于 2024-12-04 18:25:22 字数 1494 浏览 2 评论 0原文

我编写了这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-12-11 18:25:22

更多信息可能会有所帮助,同时确保 \n 始终结束输出行。 今天早上尝试运行命令,

sub runx
{
   foreach my $cmd ( @_ ) 
   {
      my $out = qx("$cmd 2>&1");
      my $x = $?;
      $out =~ s/\s*$/\n/s;
      printf "\%s (0x\%0x):\n\%s", $cmd, $x, $out;
      last if $x; 
   }
 return $x;
}

没有时间运行此代码,并且无法删除我之前的评论。但是运行“which”命令也可以确保您的命令位于 PATH 上。

More information may help, along with assuring that a \n always ends the output line. Try your running commands within

sub runx
{
   foreach my $cmd ( @_ ) 
   {
      my $out = qx("$cmd 2>&1");
      my $x = $?;
      $out =~ s/\s*$/\n/s;
      printf "\%s (0x\%0x):\n\%s", $cmd, $x, $out;
      last if $x; 
   }
 return $x;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文