Advertisement
jjeffrey

fizzbuzz-simple

Sep 28th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.94 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Settings
  4. fizz=3
  5. buzz=5
  6. input_file="$1"
  7.  
  8. # Create an empty array
  9. numbers=()
  10.  
  11. if [ "$1" = "" ] ; then
  12.   echo "Usage: $0 <input file>"
  13.   exit 1
  14. fi
  15.  
  16. while read line ; do
  17.     # Awk is a great shortcut tool for splitting a file into columns, but quotes mess it up
  18.     col1="$(echo "$line" | awk '{ print $1 }')"
  19.     # BASH has built in string replacement, but it can't do this yet...
  20.     # ... so use sed to strip out anything that isn't numeric
  21.     number="$(echo "$col1" | sed 's/[^0-9]*//g')"
  22.     numbers+=($number)
  23.     # Increment the line number for the next loop
  24.     let line_number++
  25. done < "$input_file"
  26.  
  27. # Print out the numbers, or fizz/buzz
  28. for number in ${numbers[@]} ; do
  29.     finished=0
  30.     let "${number}%${fizz}==0" && echo -n "fizz" && finished=1
  31.     let "${number}%${buzz}==0" && echo -n "buzz" && finished=1
  32.     if [ $finished -gt 0 ] ; then
  33.       echo ""
  34.     else
  35.       echo "$number"
  36.     fi
  37. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement