Shell Script Exercises requiring decisions and looping
1.Write a shell script that takes in a parameter of a filename and a directory name. If the first parameter is not a filename, print an error to the screen. If the second parameter is not a directory, print an error to the screen. If no errors, copy the file to the directory. (cp )\
2. Write a shell script that takes parameters of as many filenames as the caller wants to give. Then, it puts the first line from each of those files into another file called toplines. ($# to get the number of parms, "$@" to list the parms, for to loop, head to get the first line and >> to append to a file called toplines.)
3.Write a shell script that asks the user to type three words in, and then
tells the user the longest word. (read, wc; note that wc -m will return the
number of characters. Alternately size=${#myvar} will put the size of myvar
into the variable size. Here is an example of using wc -m to get a string size
:
echo "hello" | wc -m
This will print 6 because it is including the line feed. Just subtract 1 to
get the actual size.)
4.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. Put the names of the recipients (one per line) in a file called names, create a texfile called template which has NAME wherever you want the person's name to appear and write a script (using sed) to produce a temporary file called letter from the template file. Your shell script needs to read each person and execute the sed script for that person, appending the letters one after the other.
Note that #3 builds on the prior homework. You can get a list of names by using `cat yourNamesFile` . You can force the system to read lines together using IFS=$'\n' anytime before the cat command.
Exercises based upon http://www-h.eng.cam.ac.uk/help/tpl/unix/scripts/node16.html