LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-02-2012, 07:07 AM   #1
leniviy
Member
 
Registered: Jul 2009
Distribution: Archlinux
Posts: 37

Rep: Reputation: 2
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'
 
Old 10-02-2012, 08:38 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,494

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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
}
 
Old 10-04-2012, 08:49 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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. )

Last edited by David the H.; 10-04-2012 at 08:51 AM.
 
Old 10-05-2012, 01:24 AM   #4
leniviy
Member
 
Registered: Jul 2009
Distribution: Archlinux
Posts: 37

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by ntubski View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sql: put "beginning of line" and "end of line" within a charset... masavini Programming 7 09-19-2012 08:03 AM
getting error while running" xgraph Error in file `out.tr' at line 23930: " albho Linux - Newbie 2 04-02-2012 12:00 PM
What i s this error pop-up: "On line 3: unknown type "evolution""?? kline Linux - Desktop 0 11-25-2011 04:12 PM
Backup Script error "line 31: syntax error: unexpected end of file" eswanepoel General 7 12-07-2007 09:28 AM
Simple bash script "unexpected end of line error" snowman81 Programming 11 11-11-2007 09:31 AM


All times are GMT -5. The time now is 02:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration