代码之家  ›  专栏  ›  技术社区  ›  Vijay

如何将值从Perl程序返回到KornShell脚本?

  •  3
  • Vijay  · 技术社区  · 14 年前

    我想在shell脚本中执行此操作:

    #!/usr/bin/ksh
    
    PERL_PATH=/usr/local/bin/perl
    
    RET1=$(${PERL_PATH} missing_months_wrap.pl)
    
    echo $RET1
    

    我该怎么做?

    如上所述调用Perl脚本会给我一个错误:

    > shell.sh
    Can't return outside a subroutine at missing_months_wrap.pl line 269.
    

    编辑:在Perl脚本中,语句是:

    unless (@pm1_CS_missing_months)
    {
    $SETFLAG=1;
    }
    
    my @tmp_field_validation = `sqlplus -s $connstr \@DLfields_validation.sql`;
    
    unless (@tmp_field_validation)
    {
    $SETFLAG=1;
    }
    
    if ($SETFLAG==1)
    {
    return $SETFLAG;
    }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Greg Bacon    14 年前

    分配给 RET1 在shell脚本中,运行perl命令并捕获其标准输出。要使Perl程序继续运行,请将末尾的条件更改为

    if ($SETFLAG==1)
    {
      print $SETFLAG;
    }
    

    运行它会产生

    1

    另一种方法是使用Perl程序的退出状态。用 shell.sh 包含

    #! /usr/bin/ksh
    RET1=$(${PERL_PATH} missing_months_wrap.pl)
    echo $?
    

    改变最后一个条件 missing_months_wrap.pl

    if ($SETFLAG==1)
    {
      exit $SETFLAG;
    }
    

    您得到相同的输出:

    $ PERL_PATH=/usr/bin/perl ./shell.sh 
    1
        2
  •  2
  •   Paul R    14 年前

    您需要修改Perl脚本,以便它输出您需要的值(到stdout),然后您可以在shell脚本中使用该值。

        3
  •  2
  •   philant    14 年前

    shell脚本可以从$中的Perl脚本中检索退出状态。变量,或者Perl脚本的输出(如果使用backticks或subshell调用)。

    perl test.pl
    VAR=$?
    

    一定要拿到美元?值在Perl脚本调用之后,因为它可能会更改。

    VAR=`perl test.pl`
    

    或 var=$(perl test.pl)

    对于第二种方法,变量可以是字符串,对于第一种方法,变量必须是介于0和255之间的整数值。