【发布时间】:2014-03-18 13:44:56
【问题描述】:
我正在尝试编写一个函数,逐行检查文本文件,按某些条件检查每个字段,然后将其全部汇总。我使用完全相同的方式对每一个 cretirias 求和,但是对于第四个(在代码中是时间)我在标题中得到错误。我尝试删除总结时间的行,我的代码工作得很好,我不知道这行有什么问题,而且我对 Bash 还是很陌生。我们将不胜感激每一点帮助!
代码如下:
#!/bin/bash
valid=1
sumPrice=0
sumCalories=0
veganCheck=0
sumTime=0
function checkValidrecipe
{
while read -a line; do
if (( ${line[1]} > 100 )); then
let valid=0
fi
if (( ${line[2]} > 300 )); then
let valid=0
fi
if (( ${line[3]} != 1 && ${line[3]} != 0 )); then
let valid=0
fi
if (( ${line[3]} == 1)); then
veganCheck=1
fi
let sumPrice+=${line[1]}
let sumCalories+=${line[2]}
let sumTime+=${line[4]}
done < "$1"
}
checkValidrecipe "$1"
if (($valid == 0)); then
echo Invalid
else
echo Total: $sumPrice $sumCalories $veganCheck $sumTime
fi
我可以假设每个输入文件都采用以下格式:
name price calories vegancheck time
我正在尝试使用此输入文件运行脚本:
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
(包括空行)
这是输出:
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
Total: 270 186 1 0
非常感谢您的帮助!
【问题讨论】:
标签: bash