Bash从函数中提取元素

Bash从函数中提取元素,bash,function,Bash,Function,我很想知道是否可以从bash中的函数中提取行。假设我有这个功能: error_fn() { echo "You need at least 2 command line arguments!" echo "Program existing because you said you had a typo, please try agian" echo "Sorry, one or both of the files that you entered was a direct

我很想知道是否可以从bash中的函数中提取行。假设我有这个功能:

error_fn()
{
    echo "You need at least 2 command line arguments!"
    echo "Program existing because you said you had a typo, please try agian"
    echo "Sorry, one or both of the files that you entered was a directory, please try agian"
    echo "Sorry, one or both files were not located, please try again"
}
是否有方法从此数组中提取第一个echo语句(echo“您至少需要2个命令行参数!”)

我试过使用:

error_fn $1
error_fn ("$1")

但这似乎只是输出函数中的所有echo语句。有什么想法吗?

你可以做如下类似的
error\u fn()

error_fn() {
    array=( "You need at least 2 command line arguments!"
            "Program existing because you said you had a typo, please try agian"
            "Sorry, one or both of the files that you entered was a directory, please try agian"
            "Sorry, one or both files were not located, please try again" )
    idx=$(($1))
    if [ '0' -le "$idx" -a "$idx" -le '3' ]
    then
        printf "error: %s\n" "${array[idx]}"
    else
        printf "error: function index '%d' out of range.\n" "$idx"
    fi
}
仔细看一下,如果有问题请告诉我

对于一般错误/使用功能,我通常希望锅炉板尽可能多地位于heredoc中,同时能够通过函数的
$1
传递错误消息。例如:

usage() {

    local ecode=${2:-0}     ## set exit code '0' by default

    test -n "$1" && printf "\n %s\n" "$1" >&2  ## print error msg (if any)

## heredoc of usage information
cat >&2 << USG

usage: ${0//*\//} _required_input_, etc..

This script ... (give description)

Options:

    -h  |  --help  program help (this file)

USG

    exit $ecode;
}

您可以执行与以下类似的
error\u fn()
,并将索引值
idx
(以零为基础,例如
0-3
)作为
error\u fn()
的参数,以获得相应的错误输出

error_fn() {
    array=( "You need at least 2 command line arguments!"
            "Program existing because you said you had a typo, please try agian"
            "Sorry, one or both of the files that you entered was a directory, please try agian"
            "Sorry, one or both files were not located, please try again" )
    idx=$(($1))
    if [ '0' -le "$idx" -a "$idx" -le '3' ]
    then
        printf "error: %s\n" "${array[idx]}"
    else
        printf "error: function index '%d' out of range.\n" "$idx"
    fi
}
仔细看一下,如果有问题请告诉我

对于一般错误/使用功能,我通常希望锅炉板尽可能多地位于heredoc中,同时能够通过函数的
$1
传递错误消息。例如:

usage() {

    local ecode=${2:-0}     ## set exit code '0' by default

    test -n "$1" && printf "\n %s\n" "$1" >&2  ## print error msg (if any)

## heredoc of usage information
cat >&2 << USG

usage: ${0//*\//} _required_input_, etc..

This script ... (give description)

Options:

    -h  |  --help  program help (this file)

USG

    exit $ecode;
}

该函数只是创建输出的另一个命令。你可以解析它

error_fn | head -n 1
但是,如果您重写函数以获取参数并显示与该参数相关的错误消息,它可能会执行您希望它执行的操作:

function error_fn {
  err="$1"

  case "$err" in
    EREFP) echo "Error: refining particles failed" >&2 ;;
    ECYCE) echo "Error: number of cycles exceeded" >&2 ;;
    EHUPM) echo "Error: hangup misdirected, dangling receiver" >&2 ;;
    *)     echo "Error: something's wrong (code:$err)" >&2 ;;
  esac
}

if ! refine_particles; then
  error_fn EREFP
else if ! hang_up_projections; then
  error_fn EHUPM
fi

该函数只是创建输出的另一个命令。你可以解析它

error_fn | head -n 1
但是,如果您重写函数以获取参数并显示与该参数相关的错误消息,它可能会执行您希望它执行的操作:

function error_fn {
  err="$1"

  case "$err" in
    EREFP) echo "Error: refining particles failed" >&2 ;;
    ECYCE) echo "Error: number of cycles exceeded" >&2 ;;
    EHUPM) echo "Error: hangup misdirected, dangling receiver" >&2 ;;
    *)     echo "Error: something's wrong (code:$err)" >&2 ;;
  esac
}

if ! refine_particles; then
  error_fn EREFP
else if ! hang_up_projections; then
  error_fn EHUPM
fi

error_fn | head-n1
?这实际上会拉第一个,但只拉第一个。但是稍后在我的代码中,我想调用第二个元素,根据我所知道的,head限制了你所能看到的。因此,如果我使用head-n2,它将显示两行,而不仅仅是第二行。到grep的管道也可以工作,但不太一致。您可以使用
sed'2!d'
sed'3!在这种情况下,d'
等。。但是您可能可以将一个参数传递给函数并使用Thank,甚至没有想到使用sed。heredoc是处理boiler plate文本的最简单方法,更易于维护。
error\u fn | head-n 1
?这实际上会拉第一个,但只拉第一个。但是稍后在我的代码中,我想调用第二个元素,根据我所知道的,head限制了你所能看到的。因此,如果我使用head-n2,它将显示两行,而不仅仅是第二行。到grep的管道也可以工作,但不太一致。您可以使用
sed'2!d'
sed'3!在这种情况下,d'
等。。但是你可以将一个参数传递给函数并使用它,谢谢,我甚至没有想到使用sed。herdoc是处理锅炉板文本最简单的方法,更容易维护。