Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 6 Advanced Programming

Example: Groupcopy

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

The details of this example are decribed in “Discussion of Example: Groupcopy”.

bool='n'
query='n'
dir='n'
################################################################
# This shell program copies all of the files in the current #
# directory to the specified directory. #
# #
# Usage: To copy all files to a specified directory, type the #
# directory as the parameter. #
# To be prompted for file copy, type the -q option #
# immediately following the gp command, then the #
# directory as the second parameter. #
# To include files in subdirectories, use the -d option.#
################################################################

#(1) test to make sure the directory parameter is included
if [ $# -eq 0 ]
then
echo "gp [-opt] to_directory"
echo "Usage: include options and a directory name"
echo "options: -q, query each file"
echo " -d, include files in subdirectories"
exit 1
fi

#(2) look for options
for i
do
case $i in
-q) query='y' ;;
-d) dir='y' ;;
-*) echo "unknown option; available options are -q, -d"
exit 1 ;;
esac
done
newdir=$1

#(3) test if parameter is a directory
if [ -d $1 ]
then
# look to see if parameter is in current directory

for g in *
do
if [ $1 = $g ]
then
bool='y'
fi
done

# if parameter is in current directory,
# fill in full path name
if [ $bool = y ]
then
newdir=`pwd`/$1
fi

#(4) begin main loop
for f in *
do
if [ $f != $1 ]
then
# test if file is a directory or regular file
if [ ! -d $f ]
then
# test if query option is used
if [ $query = y ]
then
# prompt user to respond 'y' to copy,
# or anything else to ignore
echo "copy $f? \c"
read copy# test if user wants file copied
if [ $copy = y ]
then
cp $f $newdir
echo $f copied to $newdir
fi
else

# query option not used
cp $f $newdir
echo $f copied to $newdir
fi
else

# test for -d option
if [ $dir = y ]
then

# test if user wants to copy from subdirectories
echo "copy subdirectory files from $f?\c"
read dcpy
if [ $dcpy = y ]
then
if [ $query = y ]
then
curdir=`pwd`
cd $f
gp -q -d $newdir
cd $curdir
else
curdir=`pwd`
cd $f
gp -d $newdir
cd $curdir
fi
fi
fi
fi
fi
done

#(5) parameter is not a directory
else
echo "$1 is not a directory"
exit 1
fi

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:

gp [options] directory

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).

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.