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 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 )); let valid=0 fi if (( ${line[2]} > 300 )); let valid=0 fi if (( ${line[3]} != 1 && ${line[3]} != 0 )); let valid=0 fi if (( ${line[3]} == 1)); vegancheck=1 fi let sumprice+=${line[1]} let sumcalories+=${line[2]} let 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 following format:
name price 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 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, change script handle modifying
done < "$1" to
done < <(tr -d '\r' < "$1")
Comments
Post a Comment