проблема отладки файла .bash, который разделяет и отображает баллы ученика или ученика - PullRequest
0 голосов
/ 19 октября 2018

Как видно из названия, в настоящее время у меня возникают проблемы с отладкой файла .bash.Как вы можете сказать, я все еще очень плохо разбираюсь в bash и unix в целом.Внутри входной файл, который выполняет hw3.bash, содержит следующие строки в двух столбцах:

Джек 80 Мэри 95 Майкл 60 Джеффри 90

Файл будет печатать только имена, а затем только оценки,затем наивысший балл, затем наименьший, затем ранг и т. д.

Обе строки 72 и 150 являются ошибками в программе, которые я не могу отладить.Это проблема с отступом или просто грамматическая проблема.

строка 72: неожиданный EOF при поиске подходящей строки '' '150: синтаксическая ошибка: неожиданный конец файла

#1.The first positional argument ($1) provides the file name. Assign it to a
# variable named inputfile.
inputfile=$1

#2.Test if the variable inputfile refers to a file. If not, print out a proper
# message and exit with an exit code of 1.
if [[ -e inputfile ]]; then
   echo "inputfile is not a file'
   exit 1
fi

#3.Suppose each line of the input file is made up of the first name of a 
#student followed by a score (the first name and score are separate by a
# space). Put the first names in the input file into an array called names.
record=( $(cat $inputfile | awk 'END{print NR}' ))
names=( $(cat $inputfile | awk'{print $1}'))
  echo ${names[*]}


#4.Using a similar approach, put the corresponding scores into an
# array called scores.
scores=( $(cat $inputfile | awk'{print $2}'))
  echo ${scores[*]}

#5.Print names and corresponding scores (i.e. output should look the same as
# the input file). You must use a loop to do this.
names=( $(cat $inputfile |  awk'{print $1}'))
  echo${names[*]}
scores=( $(cat $inputfile | awk'{print $2}'))
echo${names[*]}
for((i=0;i<${names[*] && $i<scores[*];i++))
  do 
   echo{$names[i]} {$scores[*]}
  done

#6.Find the highest scorer, and print the name and the score. You may assume
#that there is only one highest score.
maxVal=1
maxIndex=1
for(( i=0; i<$#names[*]} && i<${#scores[*]}; i++ ))
do
    if [[ (-n ${scores[$i]}) ]]
    then 

       if(( ${scores[$i]} > $maxValue ))
       then 
       maxVal=${scores[$i]}
       maxIndex=$i
 fi
    fi
done
echo "Highest Scorer:" ${names[$maxIndex]} " " $maxVal


#7.Find the lowest scorer, and print the name and the score. You may assume
#that there is only one lowest score.
minVal=10000
maxIndex=1
for(( i=0; i<${#names[*]} && i<${#scores[*]}; i++ ))
do
    if [[ (-n ${scores[$i]}) ]]
    then 
        if(( $minVal > ${scores[i]} ))
        then
           minVal=${scores[$i]}
           minIndex=$i;
        fi
done
echo "Lower Scorer:' ${names[$minIndex]} " " $minVal

#8.Calculate the average of all scores, and print it.
avg=0
total=0;

for(( i=0;i<${#names[*]} && i<${$#scores[*]};i++ ))
do
 if [[ (-n ${scores[$i]} ]]
    then 
        total=$(( $total + ${scores[$i]} ))
    fi



#9.Sort the arrays in terms of scores (in descending order). The final
#position of each name in the array names must match with the position of the
# corresponding score in the array scores.

m=${names[*]}
n=${scores[*]}
for(( i=0; i<$n && i<$m; i++ ))
do
  maxValue=1
  maxIndex=0
  for(( i=0; j<$n && j<$m; j++)) 
  do
     if [[ !(-z ${scores[$j]} ) ]]
then
       if (( $maxVal < ${scores[$j]} ))
       then  
          maxVal=${scores[$j]}
          maxIndex=$j

       fi 
     fi
     done
     a1[${#a1[*]}]=$maxVal
a2[${#a2[*]}]=${names[$maxIndex]}
     unset scores[$maxIndex]
  done

 for(( i=0; j<${#a2[*]} && i < ${#a1[*]; i++ ))
do
 echo ${a2[i]} ${a1[i]}
done

#10.Print sorted names and scores. Put the rank of each student before the
m=${#names[*]}
n=${#scores[*]}
for (( i=0; i<$n && i<$m; i++ ))
do 
maxVal=1
maxIndex=0
for (( j=0: j<$n && j<$m; j++ ))
do
  if [[ !_-z ${scores[$j]}) ]]
  then 
    if (( $maxVal < ${scores[$j]}
    maxIndex=$j
    fi 
  fi
  done
a1[${#a1[*]}]=$maxVal
a2[${#a2[*]}=${names[$maxIndex]}
unset scores[$maxIndex]
done
k=1 
while [[ $k -lt ${#a2[*]} ]]
do
  for(( i=0;i < ${#a2[*]} && i < ${#a1[*]};i++ ))
  do

    echo "Ranking" $k "is:" ${a2[i]}
    k=$(($k+1))
  done
done

1 Ответ

0 голосов
/ 19 октября 2018

Как вы можете видеть в своей строке, перед самой , затем единицей вы смешали одинарные и двойные кавычки:

echo "inputfile is not a file'

Просто замените последнюю простую цитату на двойную.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...