Shell Script Exercises that do not require Loop or IF

1.Read in two numbers from the keyboard and display their product. (see the read, echo and let commands in man. Remember (( )) will make your variable act as a number. ).

2.Write a shell script that does not care how many parameters it gets. It should just print the number of parameters you entered and exit. (This means that if you call ./myscript a b c, it should print : you entered 3 parameters. " .)

3.Write a shell script that takes in a parameter of a command name. It should then tell you how many times you used that command in your history log. (history, grep, wc) Because history is not available when you launch a new script from your shell, you have to run this script with : source ./your script name

4. Write a shell script that takes in 2 parameters, and expects both to be file names. The script should tell you how many lines are in each file. Then, it should add both those numbers together and display the total number of lines in both files together. (wc, and you need to know how to put the return value of a command into a variable. Remember that surrounding a command with curly quotes (below escape key) will return the value. If you use cat $yourfilename | wc -l, it will return a single number, so you can set the result of that command into your variable.

5.Suppose that you want to write the same letter to many people except that you want each letter addressed to the senders personally. This mailmerge facility can be created using a shell script. In this homework, we are only doing step 1 which is:

For example:

Your template file might hold:

Hi NAME

I want to tell you about a new mobile application we are offering.

It will make run a little robot that will make you coffee in the morning and deliver it to you in bed.

Please buy this.

regards,

Professor Pepper

You run your script:

Enter the name of the recipient: Mary Jones

Expected result:

Hi Mary Jones

I want to tell you about a new mobile application we are offering.

It will make run a little robot that will make you coffee in the morning and deliver it to you in bed.

Please buy this.

regards,

Professor Pepper

 

Exercises based upon http://www-h.eng.cam.ac.uk/help/tpl/unix/scripts/node16.html