Skip to content

Instantly share code, notes, and snippets.

@OnixIsThePewterGod
Created November 23, 2019 16:26
Show Gist options
  • Save OnixIsThePewterGod/1c4c4213c294f7a608a39c07be530f91 to your computer and use it in GitHub Desktop.
Save OnixIsThePewterGod/1c4c4213c294f7a608a39c07be530f91 to your computer and use it in GitHub Desktop.
My 99 bottles of beer program in bash - simply structured
# My 99 bottles of beer program, by PokemonCoder @ repl.it and Github
# Simply structured, includes a "do you want to play it again?" statement
# Of course, there are 99 bottles (you can change this variable and it will still work!)
bottles=99
# This will be used if the user wants to exit or not
out_of_here=0
# Make sure the user stays in unless they say so (more on this later!)
while [ "$out_of_here" -lt 1 ]
do
# Variable for loop that adds 1 to the "stay less than" number, so it does not exit early
for i in $(eval echo {1..$((bottles+1))} )
do
# This large if ladder contains the main lines of the song, and checks the bottles variable lots of times
if [ "$bottles" -gt 2 ]
then
echo "$bottles bottles of beer on the wall,"
sleep 1
echo "$bottles bottles of beer."
sleep 1
echo "Take one down and pass it around,"
bottles=$((bottles-1))
sleep 1
echo "$bottles bottles of beer on the wall."
echo
sleep 1
elif [ "$bottles" = 2 ]
then
echo "$bottles bottles of beer on the wall,"
sleep 1
echo "$bottles bottles of beer."
sleep 1
echo "Take one down and pass it around,"
bottles=$((bottles-1))
sleep 1
echo "$bottles bottle of beer on the wall."
echo
sleep 1
elif [ "$bottles" = 1 ]
then
echo "$bottles bottle of beer on the wall,"
sleep 1
echo "$bottles bottle of beer."
sleep 1
echo "Take one down and pass it around,"
bottles=$((bottles-1))
sleep 1
echo "No more bottles of beer on the wall."
echo
sleep 1
# Resets the bottles variable before the very last line
elif [ "$bottles" -eq 0 ]
then
echo "No more bottles of beer on the wall,"
sleep 1
echo "no more bottles of beer."
sleep 1
echo "Go to the store and buy some more,"
bottles=$((bottles+99))
sleep 1
echo "$bottles bottles of beer on the wall."
echo
sleep 1
fi
done
# Checks if the user wants to exit, which results in the above while loop giving way
echo "Would you like to play it again? y/n"
read yn
if [ "$yn" = "y" ]
then
echo "Ok, playing again..."
sleep 1
else
echo "Exiting..."
sleep 1
out_of_here=$((out_of_here+1))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment