awk and Unary Operators in Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk and Unary Operators in Bash
# 1  
Old 02-23-2018
awk and Unary Operators in Bash

I am getting the following error

Code:
/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

Code:
  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 \
# 2  
Old 02-23-2018
Quote:
Originally Posted by kristinu
I am getting the following error

Code:
/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

Code:
  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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-24-2018
It was quite difficult to find the problem with the code though.
# 4  
Old 02-24-2018
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
Code:
hh=`
  awk -v tm=$ftm '
    BEGIN {
      printf "%.0f", int(tm/(60.0*60.0))
    }
  '
`

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk conditional operators- lookup value in 2nd file

I use conditional operators alot in AWK to print rows from large text files based on values in a certain column. For example: awk -F '\t' '{ if ($1 == "A" || $1 == "C" ) print $0}' OFS="\t" file1.txt > file2.txt In this case every row is printed from file1 to file2 for which the column 1... (5 Replies)
Discussion started by: Geneanalyst
5 Replies

2. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

3. UNIX for Beginners Questions & Answers

Bash -o -v -R operators

I do not know the use of the -o -v -R operators. This is what the info says and I am confused of what optname and varname mean, are they just normal variable? -o optname True if the shell option optname is enabled. See the list of options under the ... (6 Replies)
Discussion started by: kristinu
6 Replies

4. Programming

Need help and explanation on Unary operators

Hello guys. It's orszhak and since I'am starting in programming in c++ I again have stumbled upon something. While I was reading my book it talked about unary operators. I understood that they consist of +, -,++,--. I just want to know what do they do and how do they work. Plus it also talked of... (1 Reply)
Discussion started by: orszhak
1 Replies

5. Shell Programming and Scripting

unary operator is missing

hi , i m getting an error unary operator is missing. pls check why is this so? i=5 while test $i !=0 do echo $i i=`expr $i - 1` done (3 Replies)
Discussion started by: angel12345
3 Replies

6. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

7. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

8. Shell Programming and Scripting

unary operator expected

Im trying to fix my /etc/weekly that rotates various logs however it does them no matter what filesize they are and i want them to only do it if there file size exceeds 2M or something. So I'm playing with a script to get the filesize using a ls -l command which works and puts the value into a... (3 Replies)
Discussion started by: timgolding
3 Replies

9. Shell Programming and Scripting

awk returning "[: ==: unary operator expected"

Hi All, I am new to shell scripting and right now I am just limited to using the pre-written scripts. I am in to Infrastructure management where we use different scripts to get the information passed on to the monitoring tools. I am trying to use this script to get the information about the... (2 Replies)
Discussion started by: theamrit
2 Replies
Login or Register to Ask a Question