Discussion of Example: Groupcopy |
 |
Since this is a rather lengthy example, we have provided comments
throughout to explain its function. The example is really a new
command you can use, and you may find it quite useful. The example,
called gp, (groupcopy)
copies files from one directory into another. This will save you
time in typing each file individually as you copy the files.
The file has several options, you include options by typing
a - (minus) followed
by a letter: -q
will prompt you as each file is about to be copied, and you can
choose not to copy it; -d
will look in subdirectories if that directory has any, and then
copy it to the new directory. If no options are included, all files
in the directory (not including subdirectories) are copied to the
new directory. The format for the command is:
where options are those described
above, and directory is the directory
to where you want the files copied. The program looks in the current
directory for files to copy.
#(1) test to make sure the directory parameter
is included. The first condition (if [ $# -eq 0 ])
looks to see if the user included any options or a directory. If
they did not, they are told how the gp
command is used and the program ends.
#(2) look for options. The next section (look for options) is a for
loop with a case.
This construct looks for options. If none are found, the default
is assumed: copy all files from the current directory to the directory
specified. If options are found, an appropriate flag is set, and
the positional parameters are shifted.
#(3) test if parameter is a directory. If the parameter is a directory, check if it is in the current
directory, and set the "bool"
flag (then in the next construct concatenate the entire path name
to the parameter name; this is needed when a subdirectory is being
accessed).
#(4) begin main loop. The main loop tests several options and executes the appropriate
action. For example, if the query option (-q)
is set, it asks the user if he/she wants a file to be copies or
not.
#(5) parameter is not a directory. Finally, if the parameter supplied is not a directory, an
error message is returned.
Study the example and read the comments in the code. Then
type it into a file and try to run the program yourself. By typing
it in, you may come to understand the constructs and how they operate
better than just reading the code on a page in a manual. Some additions
you may wish to try are to selectively copy files that have a .c
suffix (C source files).