Skip to content

Instantly share code, notes, and snippets.

@Genki-S
Last active August 29, 2015 14:11
Show Gist options
  • Save Genki-S/39759b6186d27a70a361 to your computer and use it in GitHub Desktop.
Save Genki-S/39759b6186d27a70a361 to your computer and use it in GitHub Desktop.
fizzbuzz bash
number_to_fizzbuzz()
{
if [ $# -ne 1 ]; then
echo "Usage: $FUNCNAME NUMBER"
return 1
fi
local output=""
local number=$1
if [ $(($number % 3)) -eq 0 ]; then
output="${output}fizz"
fi
if [ $(($number % 5)) -eq 0 ]; then
output="${output}buzz"
fi
if [ -z "$output" ]; then
output="$number"
fi
echo $output
}
fizzbuzz()
{
if [ $# -ne 2 ]; then
echo "Usage: $FUNCNAME START END"
return 1
fi
local start=$1
local end=$2
for i in $(seq $start $end); do
number_to_fizzbuzz $i
done
}
fizzbuzz 1 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment