当我在脚本上运行ShellCheck时,会出现以下错误:
Line 27:
{
^-- SC1009: The mentioned syntax error was in this brace group.
Line 30:
for in `cat popconfile`
^-- SC1073: Couldn't parse this for loop. Fix to allow more checks.
^-- SC1058: Expected 'do'.
^-- SC1072: Expected 'do'. Fix any mentioned problems and try again
脚本为:
#!/bin/bash
#getalldata() {
#find . -name "ff_*" -exec sed -n '4p' {} \;
#}
#Defining where the population configuration file is which contains all the data
popconfile=$HOME/testarea
#Function to find the average population of all of the files
averagePopulation()
{
total=0
list=$(cat popconfile)
for var in "${list[@]}"
do
total=$((total + var))
done
average=$((total/$(${#list[*]} popconfile)))
echo "$average"
}
#Function to find the maximum population from all the files
maximumPopulation()
{
max=1
for in `cat popconfile`
do
if [[ $1 > "$max" ]]; then
max=$1
echo "$max"
fi
done
}
#Function to find the minimum population from all the files
minimumPopulation()
{
min=1000000
for in `cat popconfile`
do
if [[ $1 < "$min" ]]; then
max=$1
echo "$min"
fi
done
}
#Function to show all of the results in one place
function showAll()
{
echo "$min"
echo "$max"
echo "$average"
}
尽管我的min
函数非常相似,但没有错误。如果我将min
和max
函数切换到另一个位置,则会报告首先出现的函数的错误。
错误只是说“ 期望do
”-但是我已经有一条do
语句。那么错误在哪里?
答案 0 :(得分:3)
您缺少for
循环中的索引。立即修复是
maximumPopulation()
{
max=1
for i in `cat popconfile`
do
if [[ $i > "$max" ]]; then
max=$i
echo "$max"
fi
done
}
但是,您不应该使用for
循环来遍历文件的行;参见Bash FAQ 001。而是使用while
循环。
maximumPopulation () {
max=1
while IFS= read -r i; do
if (( i > max )); then
max=$i
fi
done < popconfile
printf '%d\n' "$max"
}