Select state
This extends the earlier "functions and file traversal" exercise you did. There, you worked with states.csv and wrote four functions in a script named filetraversal.sh. Here, create a script named selectstate.sh and in it retain those functions. The earlier exercise output a series of 50 lines, each declaring what the capital city of a state is:
The capital of the great state of Alabama is Montgomery.
The capital of the great state of Alaska is Juneau.
The capital of the great state of Arizona is Tuscon.
The capital of the great state of Arkansas is Little Rock.
The capital of the great state of California is Sacramento.
The capital of the great state of Colorado is Denver.
etc.
Here instead I want you to ask the user which single state interests him, and output the capital city declaration for just that one state. You will mainly need to learn
- how to solicit and get user input, and
- how to narrow the attention of the line that printed capital city
declarations down to a single state.
For the first, use the read built-in to print a prompt and put the user's keyboard response into a variable named "choice". The user is supposed to respond with the unique abbreviation of his desired state (upper case). The documentation for read is at about line 4500 in the
bash man page. Note the -p option. It is also described in the write-up "Shell script basics".For the second, you need to separate out from the states.csv file the single line that contains the users $choice. To do that use grep, specifying the user's choice as grep's target text and states.csv as its target file. That will get you the one line. Embed that in command substitution syntax ( $(...) ) to generate the line, submit it to your functions as their argument so they will echo the particular item (population, capital, etc) that you want.
Here is a screenshot of the behavior:
To turn in:
Upload your selectstate.sh script file. To grade it, I will supply the states.csv and reply to your script's prompt with the abbreviation of some state to observe that you print me a correct declaration of its capital.