【问题标题】:"Too late for "-C" option" error With Perl and Shell scripts使用 Perl 和 Shell 脚本时出现“-C”选项为时已晚”错误
【发布时间】:2018-01-22 09:13:10
【问题描述】:

我有一个 jar 应用程序,它有几个功能,其中之一是将 HTML 转换为 XML。当我尝试运行一个简单的命令时,例如:

java -jar lt4el-cmd.jar send -l en "l2:https://en.wikipedia.org/wiki/Personal_computer"

我收到以下错误:

ERROR [Thread-1]: html2base/html2base-wrapper.sh: Too late for "-C" option at html2base/html2xml.pl line 1.
/tmp/lpc.30872.html: failed 
cat: /tmp/lpc.30872.xml: No such file or directory
 (LpcControl.java:229)
ERROR [Thread-1]: ana2ont/ana2ont.sh ${lang}: -:1: parser error : Document is empty
-:1: parser error : Start tag expected, '<' not found
Tokenization/tagging failed
^
-:1: parser error : Document is empty
unable to parse -
-:1: parser error : Document is empty
unable to parse -
 (LpcControl.java:229)
ERROR [Thread-1]: Error in conversion: Error running conversion script (ana2ont/ana2ont.sh ${lang}): 6 (AppInterface.java:159)

这是html2base-wrapper.sh 脚本,似乎是第一个错误发生的地方。

#!/bin/bash

if [ "$1" == "check" ]; then
    . common.sh
    check_binary perl || exit 1
    check_perl_module HTML::TreeBuilder || exit 1
    check_perl_module XML::LibXML || exit 1
    check_binary tidy || exit 1
    check_binary xmllint || exit 1
    check_binary xsltproc || exit 1
    exit
fi

cat >"$TMPDIR/lpc.$$.html"
html2base/html2base.sh -d html2base/LT4ELBase.dtd -x html2base/LT4ELBase.xslt -t "$TMPDIR/lpc.$$.html" >&2
cat "$TMPDIR/lpc.$$.xml";
rm -f "$TMPDIR"/lpc.$$.{ht,x}ml

还有html2base.sh 脚本:

#!/bin/bash
#
# Sample script for automated HTML -> XML conversion
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2base.sh 462 2008-03-17 08:37:14Z qiq $

basedir=`dirname $0`;

# constants
HTML2XML_BIN=${basedir}/html2xml.pl
ICONV_BIN=iconv
TIDY_BIN=tidy
XMLLINT_BIN=xmllint
XSLTPROC_BIN=xsltproc
DTDPARSE_BIN=dtdparse
TMPDIR=/tmp

# default values
VERBOSE=0
ENCODING=
TIDY=0
VALIDATE=0
DTD=${basedir}/LT4ELBase.dtd
XSLT=${basedir}/LT4ELBase.xslt

usage()
{
        echo "usage: html2base.sh [options] file(s)"
        echo "XML -> HTML conversion script."
        echo
        echo "  -e, --encoding=charset  Convert input files from encoding to UTF-8 (none)"
        echo "  -d, --dtd=file      DTD to be used for conversion and validation ($DTD)"
        echo "  -x, --xslt=file     XSLT to be applied after conversion ($XSLT)"
        echo "  -t, --tidy      Run HTMLTidy on input HTML files"
        echo "  -a, --validate      Validate output XML files"
        echo "  -v, --verbose       Be verbose"
        echo "  -h, --help      Print this usage"
    exit 1;
}

OPTIONS=`getopt -o e:d:x:tahv -l encoding:,dtd:,xlst,tidy,validate,verbose,help -n 'convert.sh' -- "$@"`
if [ $? != 0 ]; then
    usage;
fi
eval set -- "$OPTIONS"
while true ; do
    case "$1" in
    -e | --encoding)    ENCODING=$2; shift 2 ;;
    -d | --dtd)     DTD=$2; shift 2 ;;
    -x | --xslt)        XSLT=$2; shift 2 ;;
    -t | --tidy)        TIDY=1; shift 1;;
    -a | --validate)    VALIDATE=1; shift 1;;
    -v | --verbose)     VERBOSE=1; shift 1 ;;
    -h | --help)        usage; shift 1 ;;
    --) shift ; break ;;
    *) echo "Internal error!" ; echo $1; exit 1 ;;
    esac
done

if [ $# -eq 0 ]; then
    usage;
fi

DTD_XML=`echo "$DTD"|sed -e 's/\.dtd/.xml/'`
if [ "$VERBOSE" -eq 1 ]; then
    VERBOSE=--verbose
else
    VERBOSE=
fi

# create $DTD_XML if necessary
if [ ! -f "$DTD_XML" ]; then
    if ! $DTDPARSE_BIN $DTD -o $DTD_XML 2>/dev/null; then
        echo "cannot run dtdparse, cannot create $DTD_XML";
        exit 1;
    fi;
fi

# process file by file

total=0
nok=0
while [ -n "$1" ]; do
    file=$1;
    if [ -n "$VERBOSE" ]; then
        echo "Processing $file..."
    fi
    f="$file";
    result=0;
    if [ -n "$ENCODING" ]; then
        $ICONV_BIN -f "$ENCODING" -t utf-8 "$f" -o "$file.xtmp"
        result=$?
        error="encoding error"
        f=$file.xtmp
    fi
    if [ "$result" -eq 0 ]; then
        if [ "$TIDY" = '1' ]; then
            $TIDY_BIN --force-output 1 -q -utf8 >"$file.xtmp2" "$f" 2>/dev/null
            f=$file.xtmp2
        fi
        out=`echo $file|sed -e 's/\.x\?html\?$/.xml/'`
        if [ "$out" = "$file" ]; then
            out="$out.xml"
        fi
        $HTML2XML_BIN --simplify-ws $VERBOSE $DTD_XML -o "$out" "$f"
        result=$?
        error="failed"
    fi
    if [ "$result" -eq 0 ]; then
        $XSLTPROC_BIN --path `dirname $DTD` $XSLT "$out" |$XMLLINT_BIN --noblanks --format -o "$out.tmp1" -
        result=$?
        error="failed"
        mv "$out.tmp1" "$out"
        if [ "$result" -eq 0 -a "$VALIDATE" = '1' ]; then
            tmp=`dirname $file`/$DTD
            delete=0
            if [ ! -f $tmp ]; then
                cp $DTD $tmp
                delete=1
            fi
            $XMLLINT_BIN --path `dirname $DTD` --valid --noout "$out"
            result=$?
            error="validation error"
            if [ "$delete" -eq 1 ]; then
                rm -f $tmp
            fi
        fi
    fi
    if [ "$result" -eq 0 ]; then
        if [ -n "$VERBOSE" ]; then
            echo "OK"
        fi
    else
        echo "$file: $error "
        nok=`expr $nok + 1`
    fi
    total=`expr $total + 1`
    rm -f $file.xtmp $file.xtmp2
    shift;
done
if [ -n "$VERBOSE" ]; then
    echo
    echo "Total: $total, failed: $nok"
fi

以及html2xml.pl文件的开头部分:

#!/usr/bin/perl -W -C

# Simple HTML to XML (subset of XHTML) conversion tool. Should always produce a
# valid XML file according to the output DTD file specified.
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2xml.pl 461 2008-03-09 09:49:42Z qiq $

use HTML::TreeBuilder;
use HTML::Element;
use HTML::Entities;
use XML::LibXML;
use Getopt::Long;
use Data::Dumper;
use strict;

我似乎无法弄清楚问题出在哪里。 ERROR [Thread-1] 到底是什么意思? 谢谢

【问题讨论】:

  • ERROR [Thread-1] 可能意味着线程 1 中的错误
  • 第一个错误肯定在 html2base/html2xml.pl 中?
  • 你是在说还是在问? @ChrisTurner
  • 我在问 - 这是您输出中第一条错误消息的原因,但您将其归因于 html2base-wrapper.sh
  • 我不太确定。但我已将html2xml.pl 文件的一部分添加到问题中以供进一步分析。谢谢

标签: java html xml perl nlp


【解决方案1】:

错误来自于在 Perl 脚本的 shebang (#!) 行中有 -C,但没有将 -C 传递给 perl。当有人这样做时会发生这种类型的错误

perl html2base/html2xml.pl ...

而不是

html2base/html2xml.pl ...

【讨论】:

  • 我刚刚发现 perl 脚本中的选项:-C 在最新版本的 perl 中会出错。有没有办法解决这个问题,因为我是从编译的 jar 文件中运行它的?谢谢
  • 是的,您可以使用我在回答中提供的修复程序。 (请注意,Perl 中唯一的变化是开始检测调用者中已经存在的错误。)
  • 这个命令:java -jar lt4el-cmd.jar send -l en . . . 调用 perl 脚本和它需要的所有其他库。由于我没有在命令行运行perl html2base/html2xml.pl,我该如何解决?谢谢
  • 不,java 代码没有直接调用 perl 脚本——它调用的是 html2base-wrapper.sh。那正在运行 html2base/html2base.sh 我猜它运行 html2base/html2xml.pl 或者它运行另一个运行它的脚本。您必须深入研究各种脚本才能确定运行什么。
  • 您可以尝试使用perldoc.perl.org/perlrun.html#PERL5OPTvbut 设置-C,这可能会对其他Perl 脚本产生不利影响
【解决方案2】:

错误来自html2xml.pl 脚本,正如其他用户正确提到的那样。我正在运行带有默认 perl 5.22 版本的 ubuntu 16.04.2 系统。正如post 所提到的,在#! 行上使用-C 选项(从perl 5.10.1 开始)要求您还在执行时在命令行上指定它,我不知道该怎么做因为我正在运行一个 jar 文件。我安装了perlbrew,而是使用它来获取早期版本的 perl,并将我的 perl 脚本修改为:

#!/usr/bin/path/to/perlbrew/perl -W -C

# Simple HTML to XML (subset of XHTML) conversion tool. Should always produce a
# valid XML file according to the output DTD file specified.
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2xml.pl 461 2008-03-09 09:49:42Z qiq $

This 也可能在使用 perlbrew 时设置 shell 脚本时派上用场。

感谢您的贡献。

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2014-11-30
    相关资源
    最近更新 更多