Guest User

Untitled

a guest
Sep 18th, 2017
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. sort list.dat > sortedlist.dat
  4.  
  5. read -p "Name: " n
  6. n=${n,,}
  7.  
  8. total=1
  9. while read line; do
  10. name[$total]="${line%%:*}"
  11. lcname[$total]="${name[$total],,}"
  12. grade[$total]="${line##*:}"
  13. ((total++))
  14. done < sortedlist.dat
  15.  
  16. binsearch() {
  17. local firtnum=$1
  18. local lastnum=$2
  19.  
  20. middle=$((firstnum + $(($((lastnum-firstnum))/2)) ))
  21.  
  22. if [ "${lcname[$middle]}" = "$n" ]; then
  23. echo "${name[$middle]}'s grade is ${grade[$middle]}."
  24. exit 0
  25. elif [ "$lastnum" = "$middle" ]; then
  26. echo "$n is not in the list."
  27. exit 1
  28. elif [ "$firstnum" = "$middle" ]; then
  29. echo "$n is not in the list."
  30. exit 1
  31. elif [[ "$n" > "${lcname[$middle]}" ]]; then
  32. firstnum=$middle
  33. elif [[ "$n" < "${lcname[$middle]}" ]]; then
  34. lastnum=$middle
  35. fi
  36. binsearch $firstnum $lastnum
  37.  
  38. }
  39.  
  40. binsearch 1 $total
Advertisement
Add Comment
Please, Sign In to add comment