Shell Programming Homework #1
You will create a list and write a script to lookup a row in your list. Your
program will ask the user for a searchword and then
display at least one matching column in a list you created.
- Create your list file:
- First, think of
something you might want to keep a list of. It needs at least 2 columns.
You might want actors and their movies, or games and the device you use
to play them. Please don't choose CDs because that is what your book
chose. I am expecting you to use pages 87 and 88 in your book as a guide.
- Use vi
to create a file of this list with a space separating each column. If
your data has more than one word, use _ instead of space. We will deal
with spaces inside strings later.
- Create a script that does the following:
- ask the user to enter a
search word
- tell the user how many
times that word was found in your list, (To do that: use grep to search and pipe that to wc
-l to count the number of lines in the file.)
- Say "sorry it was
not found" if you cannot find the word in your list. If the wc -l is 0, you know it was
not found.
- Tell the user all the
columns that matched for all the rows that matched (
While sed will do this easily, I want
you to instead use the script file redirection to a loop to read through
the file , parsing each line.)
- to read the file in a
loop use the following syntax:
while read col1 col2 (as many as you have)
do
# echo the label and value here
done < yourListFile
- Extra help:
- Example of a working
file read: wget http://home.adelphi.edu/~pe16132/csc271/note/scripts/numberit
- Tracing to see the
variable values as it executes, run as bash -x yourShellFilename
- If you are getting odd
read results, you might have \r carriage returns inside your file. See
that with "od -c yourShellFilename
| more " and then fix it in notepad++ with edit / eol conversion / unix
format.
For example:
My cd list:
- bat_out_of_hell meatloaf
- heaven reba_macintire
My program run #1:
- What word are you looking for: church
- Sorry, church was not found
My program run #2:
- What word are you looking for: meat
- There was 1 meat found
- Album: bat_out_of_hell
- Artist: meatloaf