Here are some tips for problem #2. NOTE: There are loads of permutations of how to do this problem; the way I'm thinking of, is a little bit small for actually requiring it be put into a script, but doing so would be simpler than the code itself; you'd just paste the commands into a file, add a shebang at the top, and save it. Instant script. And, I'm not sure if there are any particular requirements or rules as to how you are supposed go about this task, so my method may or may not be the most appropriate way for you to do this (maybe it isn't what your teacher wants, I don't know..).
In an effort to make this educational (i.e. make you read the documentation) I'm not going to just slap down a working example. Hopefully you can come up with a close idea, and we can work on it to fix it if it isn't working.
Anyhow, what I used is the `
find` command, to locate all the *.h files in the search path. Look at the `
find` manpage, and examine these options:
-type,
-name, and
-exec.
Use the
-type option to tell `find` what you're looking for, i.e.
files, or directories, etc.
Use the
-name option to specify what pattern of filename you are looking for, i.e
"*.h" for .h files.
Use the
-exec option to execute a command each time a "*.h" file is found.
The command I executed with `
find`'s
-exec option, is called `
grep`.
grep is used to search inside files for a certain
keyword, in this case the
keyword is "magic". To make
grep ignore case (and match upper or lowercase), use the
-i option.
Finally, I use a pipeline, represented by the (
|)
pipe symbol to pass each result from find+grep, into the `
wc` command. This will send every line containing "magic" through the pipeline, into `
wc` which is a word or line counter. It counts how many words/lines are sent to it. Using the
-l (lowercase 'L') option with `
wc` makes it count whole lines.
NOTE: Each *.h file that `
find` finds is represented by this double-brace character(s) when -
exec is used:
{}
So in our problem here, our `
grep` command will be using
{} to represent each filename that it searches inside looking for the word "magic".
Here's a template of what my command "looks like". You must put the appropriate options or keywords in place of what I show in [brackets] to make the command work; anything NOT inside [brackets] or <brackets> is exactly as it should be written:
Code:
find [/path/to/kernel/sources] [-type <type> -name <"name">] -exec [grep <option> <keyword>] {} \; [pipe] wc <option>
It would be much easier to just write the command for you, but then you don't learn anything that way, and have no cause to look at the documentation (man pages).
HINT: I got a result of 2145 lines containing the word "magic" in the sources of my 2.6.35.7 kernel. Whatever kernel version you are using, it should be somewhere in this range too, give or take 100 or so. (Please someone else can do their own test and see if I'm close to their result too!)
Good luck! Show us what you end up with and we'll help you fine-tune it. Or, if you go about it a totally different way, that's great! But please still show us what you come up with if you need further assistance with it.