Shell bash trace / debugging does not seem to display the script line

FreeBSD-12.3p5
GNU bash, version 5.1.16(0)-release (amd64-portbld-freebsd12.3)

I am working on a bash script and I wish to trace the execution to locate a problem. The default shell for the development user is also bash. I am invoking the script from the command line using:
Code:
export PS4="Line $LINENO: " ; /usr/bin/bash --noprofile --norc -x  ~/Projects/hll_powerhouse/freebsd/bash/email_invoices/email_invoices.sh -d 2022.05.25
LLine 9: basename /home/byrnejb_hll/Projects/hll_powerhouse/freebsd/bash/email_invoices/email_invoices.sh
Line 9: SCRIPT_NAME=email_invoices.sh
. . .
Line 9: case $OPTION in
Line 9: indate
Line 9: ARG_d=2022.05.25
Line 9: INV_DATE=2022.05.25
Line 9: is_valid_date 2022.05.25
Line 9: local _test_date
Line 9: _test_date=2022.05.25
Line 9: local _today
LLine 9: date +%Y%m%d
Line 9: _today=20220823
Line 9: '[' 8 -gt 10 ']'
Line 9: '[' 8 -lt 10 ']'
Line 9: echo 'Date provided, 2022.05.25 is too long. Expected yyyy.mm.dd format'
Date provided, 2022.05.25 is too long. Expected yyyy.mm.dd format
Line 9: exit 4

Now, it is quite evident that Line 9 is not the line number of the script where the problem is encounted. Using the textual context of the error message this is positively identified to be in this portion of the script:
Code:
   400      # check for eight digit date - \${#<string_var>} gives length of string
   401      if [ 8 -gt "${#_test_date}" ]
   402      then
   403        echo "Date provided, $_test_date, is too short. Expected yyyy.mm.dd format"
   404        exit 4
   405      elif [ 8 -lt "${#_test_date}" ]
   406      then
   407        echo "Date provided, $_test_date is too long. Expected yyyy.mm.dd format"
   408        exit 4
   409      fi
   410

My question is: Why is the trace displaying Line9:? Why does it not display the actual line numbers of the script?
 
Why the reversed test? It's very confusing to read:
Code:
   401      if [ 8 -gt "${#_test_date}" ]
   402      then
   403        echo "Date provided, $_test_date, is too short. Expected yyyy.mm.dd format"
   404        exit 4
   405      elif [ 8 -lt "${#_test_date}" ]
   406      then
   407        echo "Date provided, $_test_date is too long. Expected yyyy.mm.dd format"
   408        exit 4
   409      fi

This makes a lot more sense, and it's clearer what you're testing on:
Code:
   401      if [ "${#_test_date}" -lt 8 ]
   402      then
   403        echo "Date provided, $_test_date, is too short. Expected yyyy.mm.dd format"
   404        exit 4
   405      elif [ "${#_test_date}" -gt 8 ]
   406      then
   407        echo "Date provided, $_test_date is too long. Expected yyyy.mm.dd format"
   408        exit 4
   409      fi

Also, the error message is confusing:
Code:
Date provided, 2022.05.25 is too long. Expected yyyy.mm.dd format
You provided the expected format; 2022.05.25 and yet it's wrong? You're not expecting yyyy.mm.dd here, you're expecting it to be yyyymmdd (without the dots).
 
You provided the expected format; 2022.05.25 and yet it's wrong? You're not expecting yyyy.mm.dd here, you're expecting it to be yyyymmdd (without the dots).

The error message is misleading because the test itself contained an error. The message is for the user, not the programmer. The expected input is indeed yyyy.mm.dd, although yyyy-mm-dd and yyyymmdd are invisibly accepted as well. The date munging code is not shown as it is irrelevant to the question asked.
 
Back
Top