help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make "division by zero attempted" error message more informat


From: Ed Morton
Subject: Re: How to make "division by zero attempted" error message more informative?
Date: Thu, 3 Mar 2022 12:45:09 -0600
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1

I think you're thinking about this the wrong way. If you had a command like `grep 'foo' file` that you call from multiple bash scripts you wouldn't ask the grep developers to add some way to a prefix to the error message that the users can control and then the same for sed, cat, sort, and every other command. Instead you could write your code as:

   grep 'foo' file || printf '%s[%d]: failure.\n' "$0" "$LINENO" >&2

so just do the same thing with awk:

   $ cat ./tst.sh
   #!/usr/bin/env bash

   awk -e '{ print 10/$1 }' <<< 0 || printf '%s[%d]: failure.\n' "$0"
   "$LINENO" >&2

   $ ./tst.sh
   awk: cmd. line:1: (FILENAME=- FNR=1) fatal: division by zero attempted
   ./tst.sh[3]: failure.

That extra line tells you the problem was at line 3 of tst.sh. Obviously you can write a function to call in the `|| ...` leg instead of duplicating that code everywhere. Google for examples of that.

Regards,

    Ed

On 3/3/2022 12:09 PM, Peng Yu wrote:
Sorry. I accidentally sent the email to the bug mailing list. It
should be sent to the help mailing list.


On 3/3/22, Ed Morton<mortoneccc@comcast.net>  wrote:
That error message tells you the line number in the script (`line:1`),
the input file name (`FILENAME=-`), and the line number in the input
file where the error occurred (`FNR=1`) as well as telling you it's a
divide by zero and you can clearly see in your code where you're
dividing so I'm not sure what more information could be needed/provided.
The problem is that if this piece code is embedded inline in many bash
scripts, it will be very hard to find where the error is from.

Out of curiosity though - what would you like the error message to say?
Maybe there can be a prefix to the error message that the users can control?

      Ed.

On 3/3/2022 6:46 AM, Peng Yu wrote:
I see something like this. But when this kind of command is called at
multiple scripts. It is hard to tell where the error is from.

$ awk -e '{ print 10/$1 }' <<< 0
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: division by zero attempted

Is there a way to customize the error message so that I can know where
the
error is coming from? Thanks.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]