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

标签 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

最佳答案

bash 脚本中:

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

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

perl 脚本中:

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

关于xml - 无法在 shell 脚本中集成 perl 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635000/

相关文章:

java - 运行 Maven jar 文件时无法找到或加载主类

ruby - 无法安装 mysql gem/ruby-dev

android - 为什么我的应用没有出现在 Google Play 上?刚刚发表

c++ - 如何将 Qt 库连接到标准 C++ 项目?

linux - 'cp' 命令后“重启”命令无法正常工作

python while True循环最终默默地结束,如何保持它运行?

bash - 如何在调试过程中交互式地修改 shell 脚本?

windows - 如何将当前cygwin目录转换成windows格式

xml - 编写更高效的 xquery 代码(避免冗余迭代)

xml - 公共(public) SOAP WSDL 文件是否存在安全问题?