Sunday, 15 March 2015

bash - syntax error: invalid arithmetic operator (error token is "") -



bash - syntax error: invalid arithmetic operator (error token is "") -

i trying write function checks text file, line line, checking each field cretirias, , sums up. using exact same way sum each 1 of cretirias, 4th 1 (in code time) error in title. tried removing line sums time , code worked fine, have no clue what's wrong line , i'm pretty new bash. every bit of help appreciated!

here's code:

#!/bin/bash valid=1 sumprice=0 sumcalories=0 vegancheck=0 sumtime=0 function checkvalidrecipe { while read -a line; if (( ${line[1]} > 100 )); allow valid=0 fi if (( ${line[2]} > 300 )); allow valid=0 fi if (( ${line[3]} != 1 && ${line[3]} != 0 )); allow valid=0 fi if (( ${line[3]} == 1)); vegancheck=1 fi allow sumprice+=${line[1]} allow sumcalories+=${line[2]} allow sumtime+=${line[4]} done < "$1" } checkvalidrecipe "$1" if (($valid == 0)); echo invalid else echo total: $sumprice $sumcalories $vegancheck $sumtime fi

and can assume every input file in next format:

name cost calories vegancheck time

i trying run script input file:

t1 50 30 0 10 t2 10 35 0 10 t3 75 60 1 60 t4 35 31 0 100 t5 100 30 0 100

(blank line included)

and here's output:

")syntax error: invalid arithmetic operator (error token " ")syntax error: invalid arithmetic operator (error token " ")syntax error: invalid arithmetic operator (error token " ")syntax error: invalid arithmetic operator (error token " ")syntax error: invalid arithmetic operator (error token " total: 270 186 1 0

thank much help!

your input file contains cr+lf line endings. such, variable ${line[4]} isn't number 10 10\r causes error.

remove carriage returns input file using tool such dos2unix.

alternatively, alter script handle modifying

done < "$1"

to

done < <(tr -d '\r' < "$1")

bash

No comments:

Post a Comment