Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Perl脚本不执行某些外部调用_Linux_Perl_Wifi - Fatal编程技术网

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

Linux Perl脚本不执行某些外部调用,linux,perl,wifi,Linux,Perl,Wifi,我编写了这个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(STD

我编写了这个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解释器与Perl命令返回的解释器相同


有什么线索吗?提前谢谢

除了确保a\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; } 亚运行 { foreach my$cmd(@41; { my$out=qx($cmd 2>&1); 我的$x=$?; $out=~s/\s*$/\n/s; printf“\%s(0x\%0x):\n\%s”,$cmd,$x,$out; 最后如果$x; } 返回$x; }
今天早上没有时间运行此代码,无法删除我之前的评论。但有些东西运行“which”命令也可以确保您的命令在路径上。

试试my$out=qx(sudo ifconfig wlan0 down)
gksudo/sudo
提示输入密码?是。如果不是这样,任何一个命令都不会起作用。事实上,我再次修改了脚本,删除了所有sudo/gksudo并像sudo/gksudo./wicd.pl那样调用它,看起来更干净了IMHO(稍后我会编辑这个问题)。还是一样的问题。谢谢你的提示。我将尝试一下,但正如我前面所说的,这些命令在终端上或通过Perl调试器运行良好(因此路径问题是不存在的)。只有在运行像./wicd.pl这样的脚本时,问题才会发生。您还需要什么信息?当我使用qx()时,我无法执行任何命令。它总是吐出“sh:command not found”。不管它是否以\n结尾。Ok qx()需要不带引号和尾随的命令\n。反正没什么区别,还是一样的行为。