awk and Unary Operators in Bash

I am getting the following error

/home/hagbard/chaos/resip/ictja/siculet-0.0.0/amvib/tmspan.sh: line 135: BEGIN {printf "%.0f", int((tm-(thh*60.0*60.0))/60.0)}: No such file or directory
/home/hagbard/chaos/resip/ictja/siculet-0.0.0/amvib/tmspan.sh: line 138: [: -lt: unary operator expected

The code is this

  hh=`awk -v tm=$ftm  \
    'BEGIN {printf "%.0f", int(tm/(60.0*60.0))}'`
  mm=`awk -v tm=$ftm -v thh=$hh  \ 
    'BEGIN {printf "%.0f", int((tm-(thh*60.0*60.0))/60.0)}'`
  ss=`awk -v tm=$ftm -v thh=$hh -v tmm=$mm  \
    'BEGIN {printf "%.0f", int(tm-(thh*60.0*60.0)-(tmm*60.0))}'`
  [ $hh -lt 10 ] && hh="0${hh}"
  [ $mm -lt 10 ] && mm="0${mm}"
  [ $ss -lt 10 ] && ss="0${ss}"
  ftm="${hh}:${mm}:${ss}"  

---------- Post updated at 06:27 PM ---------- Previous update was at 06:18 PM ----------

What a funny thing, it happens that an error occurs whenever there are spaces after the continuation backslashes \

Why do you think that is a funny thing??? By definition, a continuation backslash is a <backslash> character IMMEDIATELY followed by a <newline> character. If there is any character between a <backslash> and a <newline>, the <backslash> provides an escape mechanism to that character and is NOT a continuation character.

1 Like

It was quite difficult to find the problem with the code though.

Sometimes the \ at the very end of the line are mistreated by copy/paste operations: a space character is appended.
Usually I break the lines where the syntax supports it. For example

hh=`
  awk -v tm=$ftm '
    BEGIN {
      printf "%.0f", int(tm/(60.0*60.0))
    }
  '
`