bash - 调试分隔和回显学生或学生分数的 .bash 文件时遇到问题

标签 bash ubuntu unix

就像标题所暗示的那样,我目前在调试 .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

最佳答案

就像您在行中看到的那样,就在 之前然后 一,你混合了单引号和双引号:

echo "inputfile is not a file'

只需将最后的简单引号替换为双引号即可。

关于bash - 调试分隔和回显学生或学生分数的 .bash 文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52884761/

相关文章:

linux - 获取 Linux 日志文件中最后 30 分钟的条目

Bash,使用文件描述符覆盖?

unix - 使用 grep 解析另一个命令的输出

bash - 将输入管道输入到 shell 命令并保持创建的 shell 处于事件状态

linux - 使用 unix 编写脚本以获取用户运行的进程

ruby-on-rails - 无法访问 docker 容器

bash 脚本错误 : file or directory doesnt exist

security - 我的 ClamAV 日志在哪里?

python - 从 python 脚本的多个终端窗口运行多个命令

linux - 为什么我不能使用下面的 shell 参数扩展提取主机名的最后三个字符?