LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shorten "ERROR line $LINENO: ..." (http://www.linuxquestions.org/questions/programming-9/shorten-error-line-%24lineno-4175430035/)

leniviy 10-02-2012 08:07 AM

Shorten "ERROR line $LINENO: ..."
 
Hi. Does anyone know a way to shorten the error message prefix with the $LINENO variable in shell scripts?

I would like to have
Code:

$fatal "FILE not specified"
instead of
Code:

    echo "ERROR line $LINENO: FILE not specified" >&2
    exit 1

But to expand $LINENO one needs to use 'eval' and I don't want to pass the other arguments to eval:
Code:

_fatal() {
  ... "$@"
}
fatal='eval _fatal $LINENO'


ntubski 10-02-2012 09:38 AM

If you are using bash, BASH_LINENO gives you an array of all the line numbers in the call stack:
Code:

fatal() {
  echo "ERROR line ${BASH_LINENO[0]}: $@" >&2
  exit 1
}


David the H. 10-04-2012 09:49 AM

I'm not following the request here. Why do you need to use eval? Why doesn't $LINENO expand on its own?

Is it because you're trying to call the function this way?

Code:

fatal='eval _fatal $LINENO'
Shouldn't you be using this instead?

Code:

fatal=$( _fatal "$LINENO" )
...which captures the stdout of the _fatal function into the variable $fatal.

How about giving us some more context, and the actual contents of the function, so we can more easily know what you're trying to do?

(As an additional point, having both a variable and a function with almost the same name is rather confusing. )

leniviy 10-05-2012 02:24 AM

Quote:

Originally Posted by ntubski (Post 4794908)
If you are using bash, BASH_LINENO gives you an array of all the line numbers in the call stack:
Code:

fatal() {
  echo "ERROR line ${BASH_LINENO[0]}: $@" >&2
  exit 1
}


Thanks


All times are GMT -5. The time now is 08:57 PM.