无法在shell脚本中集成perl脚本

时间:2015-09-17 16:03:48

标签: xml linux bash perl shell

我有一个shell脚本,其中我也有perl脚本。我需要将shell脚本中的变量值传递给perl。

这是我的perl脚本,我在其中加载文件client_23.xml文件和abc_lop.xml文件。我希望这两个值来自shell脚本,然后将$doc->toString(2);的值存储在变量或文件中,以便我的shell脚本可以在此文件上运行。

use strict;
use warnings;

use XML::LibXML;

# Open the main XML file and locate the
# <block> element that we need to insert into
#
my $doc = XML::LibXML->load_xml(
    location => 'client_23.xml',
    no_blanks => 1,
);
my $block = $doc->find('/function/block')->get_node(1);

# Open the secondary XML file and find all the <ClientField> elements
# that contain the data we need to insert
#
my $abc = XML::LibXML->load_xml(location => 'abc_lop.xml');

for my $field ( $abc->find('/Hello/DataHolder/ClientField')->get_nodelist ) {

    my ($name, $pptype) = map $field->getAttribute($_), qw/ name pptype /;

    my $text = $pptype eq 'aligning' ?
        sprintf q{upsert("%s", "NA", $calty_strings)}, $name :
        sprintf q{upsert("%s", 0, $calty_doubles)}, $name;

    $block->appendTextChild('eval' , $text);
}

print $doc->toString(2);

这是我的shell脚本,目前尚未与上面的perl集成:

for word in $client_types
do
    ## Concatenate the header, the contents of the target file and the
    ## footer into the variable $file.
    file=$(printf '%s\n%s\n%s' "$header" "$(sed 's/xsi:schemaLocation="[^"]*"//' "$file_location/${word}_lop.xml")" "$footer")

    ### I want my perl script to be here and output of that perl script should be an input to my below sed command

    ## Edit the target file and print
    sed 's|<eval>holder_clients = 1</eval>|<eval>holder_clients = 0</eval>|; \|<derta-config>|,\|</derta-config>|d' client_"$client_id".xml | perl -0pe "s#<function>\s*<name>DUMMY_FUNCTION.+?</function>#$file#sm" > "$word"_new_file.xml
done

所以我希望我的perl脚本位于我的shell脚本中间,如上所示。而不是硬编码的client_23.xml名称,我想使用client_"$client_id".xml而不是硬编码的abc_lop.xml文件,我想使用${word}_lop.xml。所以我想出了下面的脚本,它也使用perl脚本,但它不起作用,它给出了编译错误:

#!/bin/bash

readonly path_location="/export/home/rjamal/validation_test"
readonly client_id=43
readonly client_types="abc"

header='<hello_function>
<name>hello_function</name>'
footer='</hello_function>'

for word in $client_types
do
    ## Concatenate the header, the contents of the target file and the
    ## footer into the variable $file.
    file=$(printf '%s\n%s\n%s' "$header" "$(sed 's/xsi:schemaLocation="[^"]*"//' "$path_location/${word}_lop.xml")" "$footer")

    use strict;
    use warnings;

    use XML::LibXML;

    # Open the main XML file and locate the
    # <block> element that we need to insert into
    #
    my $doc = XML::LibXML->load_xml(
        location => '$path_location/client_"$client_id".xml',
        no_blanks => 1,
    );
    my $block = $doc->find('/function/block')->get_node(1);

    # Open the secondary XML file and find all the <ClientField> elements
    # that contain the data we need to insert
    #
    my $abc = XML::LibXML->load_xml(location => '$path_location/${word}_lop.xml');

    for my $field ( $abc->find('/HELLO/DataHolder/ClientField')->get_nodelist ) {

        my ($name, $pptype) = map $field->getAttribute($_), qw/ name pptype /;

        my $text = $pptype eq 'aligning' ?
            sprintf q{upsert("%s", "NA", $calty_strings)}, $name :
            sprintf q{upsert("%s", 0, $calty_doubles)}, $name;

        $block->appendTextChild('eval' , $text);
    }

    # writing into a file and thne this file will be input to my sed command below
    echo $doc->toString(2) > $path_location/client_"$client_id"_temp.xml

    ## Edit the target file and print
    sed 's|<eval>data_client_holder = 1</eval>|<eval>data_client_holder = 0</eval>|; \|<delrta-config>|,\|</delrta-config>|d' "$path_location/client_{$client_id}_temp.xml" | perl -0pe "s#<function>\s*<name>hello_function.+?</function>#$file#sm" > "$word"_dyn_model.xml
done

1 个答案:

答案 0 :(得分:1)

bash脚本中:

script.pl "$client_id" "$word" >"$path_location/client_{$client_id}_temp.xml" 

(或者,如果您不需要将输出保存到文件,则可以将输出直接传送到sed。)

perl脚本中:

my ($client_id, $word) = @ARGV;