【问题标题】:Returning a filename from a ksh function gives a 'bad number' error从 ksh 函数返回文件名会产生“错误编号”错误
【发布时间】:2022-01-14 15:54:25
【问题描述】:

我有一个带有函数的 shell 脚本,当我调用该函数时,我收到了错误的数字错误。在我的函数中,我从数据库中获取一个文件名并返回它。返回给出“错误号码”错误。详情如下:

call to GET_MYFILE() :
  GET_MYFILE $jobid
--$job id has the correct number in it

GET_MYFILE()
{
        echo "job id input parameter is : " $1
        curfile=`sqlplus -s /@username<< EOF
        set feed off heading off verify off serveroutput off

        select my_file_name
        from my_table_name
        where jobid=$1;
        exit;
EOF`
        echo "curfile is : " $curfile
        return $curfile
}

-- echo "curfile is : " $curfile - displays correct filename 
--return $curfile gives bad number error.

【问题讨论】:

  • return 不能返回任意数据,只能返回整数退出状态。 Shell 函数确实不是很像函数。它们更像是小程序。
  • return 到一个函数类似于exit 到一个 shell 脚本。它设置状态($?)并返回给调用者。

标签: shell ksh


【解决方案1】:

在 Korn Shell 语法中,return 语句语法仅提供两种变体:

  • return(在这种情况下,返回的数字是函数中最近执行的命令的退出代码)。

  • return n(其中 n 是 0 到 255 之间的数字)

在任何一种情况下,调用者都可以访问通过赋值语句或条件语句中的$? 伪变量返回的数值。

如果您尝试返回文件名,那么您应该期待“错误数字”,因为文件名字符串通常不能被解释为数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多