Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- sort list.dat > sortedlist.dat
- read -p "Name: " n
- n=${n,,}
- total=1
- while read line; do
- name[$total]="${line%%:*}"
- lcname[$total]="${name[$total],,}"
- grade[$total]="${line##*:}"
- ((total++))
- done < sortedlist.dat
- binsearch() {
- local firtnum=$1
- local lastnum=$2
- middle=$((firstnum + $(($((lastnum-firstnum))/2)) ))
- if [ "${lcname[$middle]}" = "$n" ]; then
- echo "${name[$middle]}'s grade is ${grade[$middle]}."
- exit 0
- elif [ "$lastnum" = "$middle" ]; then
- echo "$n is not in the list."
- exit 1
- elif [ "$firstnum" = "$middle" ]; then
- echo "$n is not in the list."
- exit 1
- elif [[ "$n" > "${lcname[$middle]}" ]]; then
- firstnum=$middle
- elif [[ "$n" < "${lcname[$middle]}" ]]; then
- lastnum=$middle
- fi
- binsearch $firstnum $lastnum
- }
- binsearch 1 $total
Advertisement
Add Comment
Please, Sign In to add comment