In this exercise, we will be adding script code to build a simple game where the users guess the name of a song when its lyrics are outputted to the terminal. You may be wondering, how do we load in lyrics? We will take advantage of the script we ran in the introduction which fetches lyrics from the Internet. This lyrics script is a modified version of the lyrics script provided through Bash-Snippets.
We have provided a code skeleton for the script we are writing in the file named song_guesser.sh
, which is already opened in the code editor. As a quick overview, the code contains:
- A
while
loop to continuously run the game until the user wishes to stop. - A
GenerateRandomArtistAndSong
function that populates the variablesartist_name
andsong_name
with a random song name and its artist. - Statements to print the lyrics into the terminal and prompt the user to make a guess.
- Statements to tell the user whether they guessed the right song name and if they wish to play again.
To fill in the script, we will pass the song name and the artist to the lyrics.sh
script, retrieve the lyrics returned from that script, and output it in this song_guesser.sh
script. Let’s begin!
Instructions
Before we use it in our own script, let’s see how the lyrics.sh
script works. Switch the editor to the file lyrics.sh
and skim through. Let’s run the command ./lyrics.sh -h
in the terminal to see the usage.
We see that we can pass the artist_name
argument using the -a
option, and pass the song_name
argument using the -s
option.
(For compatibility and demonstration purposes, we have limited the lyrics
script to only provide lyrics to 5 songs.)
In the while loop, right after the GenerateRandomArtistAndSong
function is called and just before the line echo $lyrics
, pass the artist_name
and song_name
variables as arguments to the lyrics.sh
script. Save the output to a variable named lyrics
.
Click the Run button when you are done.
We did it! Try out the game by running the command ./song_guesser.sh
. Feel free to play it as many times as you like.