Perl文件复制复制输出

时间:2012-05-02 14:32:49

标签: regex perl file-io duplicates

我正在尝试编写一个菜单驱动的模块化perl脚本,它将捕获用户输入并自动执行网络配置过程。此脚本必须能够安装所需的Arch包,配置AP模式,为用户选择的接口配置DHCP或静态地址,并提供启用桥接的选项。 (编辑:该脚本还需要能够启用和配置dhcpd服务)

我现在停留的部分是创建rc.conf文件的备份,读取文件并编辑需要修改的行(如果已经静态配置了网络接口)。这个脚本是在ArchLinux中使用的,我做了一些搜索,没有找到任何符合我需要的东西。

使用通用输入 $ip = 1.1.1.1; $Bcast = 2.2.2.2; $netmask = 3.3.3.3; $GW = 4.4.4.4;

我花了大约两个小时阅读文件I / O并尝试了一些不起作用的事情,包括废弃多文件IO方法并使用类似于while(<IS>){s/^interface.?=(.*)$/"interface=@if[0] \n"/;}的内容,每个值都有输入需要更换,无法让它实际做任何事情。

   if (system ("cat","/etc/rc.conf","|","grep","interface")){   
    use File::Copy "cp";
    $filename = "/etc/rc.conf"; 
    $tempfile = "/etc/rc.tmp";  
    $bak = "/etc/rc.bak";
    cp($filename,$bak);
    open(IS, $filename); 
    open(OS, ">$tempfile"); 
    while(<IS>){ 
        if($_ =~ /^interface.?=(.*)$/){ print OS"interface=@if[0] \n";}
        if($_ =~ /^address.?=(.*)$/){ print OS "address=$ip\n";}
        if($_ =~/^netmask.?=(.*)$/){ print OS "netmask=$netmask\n";}
        if($_ =~/^broadcast.?=(.*)$/){ print OS "broadcast=$Bcast\n";}
        if($_ =~/^gateway.?=(.*)$/){ print OS "gateway=$GW\n"; }
        else {print OS $_;} 
    }
    close(IS); close(OS); 
    unlink($filename); rename($tempfile, $filename);
}

之前的

#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

LOCALE_DEFAULT="en_US.UTF-8"
DAEMON_LOCALE_DEFAULT="no"
CLOCK_DEFAULT="UTC"
TIMEZONE_DEFAULT="Canada/Pacific"
KEYMAP_DEFAULT="us"
CONSOLEFONT_DEFAULT=
CONSOLEMAP_DEFAULT=
USECOLOR_DEFAULT="yes"

LOCALE="$(kernel_cmdline locale ${LOCALE_DEFAULT})"
DAEMON_LOCALE="$(kernel_cmdline daemon_locale ${DAEMON_LOCALE_DEFAULT})"
HARDWARECLOCK="$(kernel_cmdline clock ${CLOCK_DEFAULT})"
TIMEZONE="$(kernel_cmdline timezone ${TIMEZONE_DEFAULT})"
KEYMAP="$(kernel_cmdline keymap ${KEYMAP_DEFAULT})"
CONSOLEFONT="$(kernel_cmdline consolefont ${CONSOLEFONT_DEFAULT})"
CONSOLEMAP="$(kernel_cmdline consolemap ${CONSOLEMAP_DEFAULT})"
USECOLOR="$(kernel_cmdline usecolor ${USECOLOR_DEFAULT})"

MODULES=()

UDEV_TIMEOUT=30
USEDMRAID="no"
USEBTRFS="no"
USELVM="no"

HOSTNAME="archiso"

DAEMONS=(hwclock syslog-ng)

interface=eth0
address=192.168.0.99
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1

后面的

#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

LOCALE_DEFAULT="en_US.UTF-8"
DAEMON_LOCALE_DEFAULT="no"
CLOCK_DEFAULT="UTC"
TIMEZONE_DEFAULT="Canada/Pacific"
KEYMAP_DEFAULT="us"
CONSOLEFONT_DEFAULT=
CONSOLEMAP_DEFAULT=
USECOLOR_DEFAULT="yes"

LOCALE="$(kernel_cmdline locale ${LOCALE_DEFAULT})"
DAEMON_LOCALE="$(kernel_cmdline daemon_locale ${DAEMON_LOCALE_DEFAULT})"
HARDWARECLOCK="$(kernel_cmdline clock ${CLOCK_DEFAULT})"
TIMEZONE="$(kernel_cmdline timezone ${TIMEZONE_DEFAULT})"
KEYMAP="$(kernel_cmdline keymap ${KEYMAP_DEFAULT})"
CONSOLEFONT="$(kernel_cmdline consolefont ${CONSOLEFONT_DEFAULT})"
CONSOLEMAP="$(kernel_cmdline consolemap ${CONSOLEMAP_DEFAULT})"
USECOLOR="$(kernel_cmdline usecolor ${USECOLOR_DEFAULT})"

MODULES=()

UDEV_TIMEOUT=30
USEDMRAID="no"
USEBTRFS="no"
USELVM="no"

HOSTNAME="archiso"

DAEMONS=(hwclock syslog-ng)

interface=eth0 
interface=eth0
address=1.1.1.1
address=192.168.0.99
netmask=3.3.3.3
netmask=255.255.255.0
broadcast=2.2.2.2
broadcast=192.168.0.255
gateway=4.4.4.4

2 个答案:

答案 0 :(得分:1)

问题在于您的if / if / if / if / if / else链,该链应为{{ 1}} / if / elsif / elsif / elsif / elsif链。 else触发与else { print OS $_ }不匹配的每一行,包括与gateway=interface匹配的行等。

答案 1 :(得分:1)

我不会评论你的其他剧本的智慧,但你有:

if (system ("cat","/etc/rc.conf","|","grep","interface")){ 

system成功返回0

因此,只有当system来电失败时,您才会进入该区块。

事实上,我现在在Windows系统上没有/etc/rc.confcatgrep感谢Cygwin。运行以下脚本:

#!/usr/bin/env perl

use strict; use warnings;

if (system ("cat","/etc/rc.conf","|","grep","interface")){
    print "*** it worked! ***\n";
    if ($? == -1) {
        print "failed to execute: $!\n";
    }
    elsif ($? & 127) {
        printf "child died with signal %d, %s coredump\n",
            ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
        printf "child exited with value %d\n", $? >> 8;
    }
}

产生输出:

cat: /etc/rc.conf: No such file or directory
cat: |: No such file or directory
cat: grep: No such file or directory
cat: interface: No such file or directory
*** it worked! ***
child exited with value 1

这意味着system返回了失败代码。现在,如果你想使用shell管道和重定向,你应该传递system一个字符串,而不是列表,并检查如下:

if (system ('cat /etc/rc.conf | grep interface') == 0) {

另一方面,我宁愿不相信传播退出状态的shell。

以下内容应指向更好的方向:

#!/usr/bin/env perl

use strict;use warnings;

my %lookup = (
    eth0 => {
        address => '1.1.1.1',
        broadcast => '2.2.2.2',
        netmask => '3.3.3.3',
        gateway => '4.4.4.4',
    },
    wlan0 => {
        address => '5.5.5.5',
        broadcast => '6.6.6.6',
        netmask => '7.7.7.7',
        gateway => '8.8.8.8',
    },

);

while (my $line = <DATA>) {
    if (my ($interface) = ($line =~ /^interface=(\S+)/)) {
        print $line;
        if (exists $lookup{$interface}) {
            $line = process_interface(\*DATA, $lookup{$interface});
            redo;
        }
    }
    else {
        print $line;
    }
}

sub process_interface {
    my ($fh, $lookup) = @_;
    my $keys = join '|', sort keys %$lookup;

    while (my $line = <DATA>) {
        $line =~ s/\A($keys)=.+/$1=$lookup->{$1}/
            or return $line;
        print $line;
    }

    return;
}

__DATA__
#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

# stuff

interface=eth0
address=192.168.0.99
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1
interface=wlan0
address=192.168.0.99
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1

输出:

#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

# stuff

interface=eth0
address=1.1.1.1
netmask=3.3.3.3
broadcast=2.2.2.2
gateway=4.4.4.4
interface=wlan0
address=5.5.5.5
netmask=7.7.7.7
broadcast=6.6.6.6
gateway=8.8.8.8