0

My sample code is here

#!/bin/bash
file="output2.txt"
numbers="$(cut -d',' -f2 output2.txt)"
lines="$(cut -f2 output2.txt)"
hours="$(cut -d',' -f1 output2.txt)"
array_numbers=( $numbers )
lines_array=( $lines )
hours_array=( $hours )
difference=$1
let range=$1-1000
for (( i = 0 ; i < ${#array_numbers[@]} ; i++ )) 
do
let num=$(( 10#${array_numbers[$i+1]} - 10#${array_numbers[$i]} ))
   if [ $num -gt $1 ]
     then
       echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
   elif [ $num -ge 0 ] && [ $num -lt $range ] 
     then
       echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
   elif [ $num -le $1 ]
     then
       if [${hours_array[$i+1]} != ${hours_array[$i]}]
       then
         echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than one second"
       fi
  fi
done

I'm working with the same output2.txt again:

12:43:40,317
12:43:40,318
12:43:40,332
12:43:40,333
12:43:40,334
12:43:40,335
12:43:40,336
12:43:40,337
12:43:40,338
12:43:40,339
12:43:40,353
12:43:40,354
12:43:40,356
12:43:40,358
12:43:40,360
12:43:40,361
12:43:40,362
12:43:40,363
12:43:40,364
12:43:40,365
12:43:40,382
12:43:40,384
12:43:40,385
12:43:40,387
12:43:40,388
12:43:40,389
12:43:40,390
12:43:40,391
12:43:40,404
12:43:40,405
12:43:40,406
12:43:40,407
12:43:40,408
12:43:40,409
12:43:40,410
12:43:40,412
12:43:40,413
12:43:40,414
12:43:40,415
12:43:40,428
12:43:40,429
12:43:40,431
12:43:40,432
12:43:40,433
12:43:40,434
12:43:40,435
12:43:40,436
12:43:40,437
12:43:40,438
12:43:40,440
12:43:40,443
12:43:40,458
12:43:40,459
12:43:40,460
12:43:40,461
12:43:40,462
12:43:40,463
12:43:40,464
12:43:40,465
12:43:40,466
12:43:40,479
12:43:40,480
12:43:40,481
12:43:40,482
12:43:40,483
12:43:40,484
12:43:40,485
12:43:40,486
12:43:40,487
12:43:40,501
12:43:40,503
12:43:40,504
12:43:40,505
12:43:40,506
12:43:40,509
12:43:40,510
12:43:40,511
12:43:40,512
12:43:40,513
12:43:40,514
12:43:40,515
12:43:40,517
12:44:40,518

What I want to do is take the difference as parameter and if there is a value difference more than 100 miliseconds than I'm wanna print output. The parts

for (( i = 0 ; i < ${#array_numbers[@]} ; i++ )) 
    do
    let num=$(( 10#${array_numbers[$i+1]} - 10#${array_numbers[$i]} ))
          if [ $num -gt $1 ]
              then
               echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
          elif [ $num -ge 0 ] && [ $num -lt $range ] 
              then
               echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"

are actually working well , but i realized that if input has such a columns in order like the last part

12:43:40,517
12:44:40,518

it won't print anything so i put the last elif statement to my code but even it prints hours_array good, it doesn't work with while i'm comparing them. The output is always :

script.sh: line 22: [12:43:00: command not found

Why doesn't it accept this compare or is the problem is about my bash version ?

Thank you in advance for your help.

2
  • amusingly, the time taken for reading the code would be less than posting the question here with formatting... (especially since you already know the syntax...) :D Commented Sep 30, 2014 at 13:14
  • yes I already know about this but as I said sometimes being blind is a big problem for us (!!) :D Commented Sep 30, 2014 at 13:55

4 Answers 4

1

Add space before and after [. It is an 'alias' to the test buitin command.

You should also add double quote " around your variable. Because if they are empty, bash won't recognize them as a empty word.

And I generally use double brackets [[ for test condition which is more safer and has more features.

Example:

if [[ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]]
Sign up to request clarification or add additional context in comments.

1 Comment

Great advice, but somewhat redundant -- using [[ ]] makes the quotes unnecessary, at least on the left-hand side.
0

You need a space here (the [ is a command)

 if [ ${hours_array[$i+1]} != ${hours_array[$i]} ]

1 Comment

Also quotes to be safe: if [ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]; that way, going off the end yields a false comparison rather than letting test complain about a syntax error.
0

Missing space after [. [ is a command, so it needs to be separated from its arguments.

if [ ${hours_array[$i+1]} != ${hours_array[$i]} ]

Comments

0

I find few things that can be changed in this code.

  • Add space after [ and before ].
  • Add double quotes so that in case if variable is empty, script does not throw error.

    if [ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]
    
  • Also when you reach the last line, $i + 1 will fail. Hence, following would be better.

    for (( i = 0 ; i < ${#array_numbers[@]} - 1 ; i++ ))
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.