home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / contrib / descend.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1996-09-28  |  3KB  |  117 lines

  1. #! /bin/sh
  2. # $Id: descend.sh,v 1.1 1995/07/10 02:26:32 kfogel Exp $
  3. #
  4. # descend - walk down a directory tree and execute a command at each node
  5.  
  6. fullname=$0
  7. name=descend
  8. usage="Usage: $name [-afqrv] command [directory ...]\n
  9. \040\040-a\040\040All: descend into directories starting with '.'\n
  10. \040\040-f\040\040Force: ignore errors during descent\n
  11. \040\040-q\040\040Quiet: don't print directory names\n
  12. \040\040-r\040\040Restricted: don't descend into RCS, CVS.adm, SCCS directories\n
  13. \040\040-v\040\040Verbose: print command before executing it"
  14.  
  15. # Scan for options
  16. while getopts afqrv option; do
  17.     case $option in
  18.     a)
  19.         alldirs=$option
  20.         options=$options" "-$option
  21.         ;;
  22.     f)
  23.         force=$option
  24.         options=$options" "-$option
  25.         ;;
  26.     q)
  27.         verbose=
  28.         quiet=$option
  29.         options=$options" "-$option
  30.         ;;
  31.     r)
  32.         restricted=$option
  33.         options=$options" "-$option
  34.         ;;
  35.     v)
  36.         verbose=$option
  37.         quiet=
  38.         options=$options" "-$option
  39.         ;;
  40.     \?)
  41.         /usr/5bin/echo $usage 1>&2
  42.         exit 1
  43.         ;;
  44.     esac
  45. done
  46. shift `expr $OPTIND - 1`
  47.  
  48. # Get command to execute
  49. if [ $# -lt 1 ] ; then
  50.     /usr/5bin/echo $usage 1>&2
  51.     exit 1
  52. else
  53.     command=$1
  54.     shift
  55. fi
  56.  
  57. # If no directory specified, use '.'
  58. if [ $# -lt 1 ] ; then
  59.     default_dir=.
  60. fi
  61.  
  62. # For each directory specified
  63. for dir in $default_dir "$@" ; do
  64.  
  65.     # Spawn sub-shell so we return to starting directory afterward
  66.     (cd $dir
  67.  
  68.     # Execute specified command
  69.     if [ -z "$quiet" ] ; then
  70.         echo In directory `hostname`:`pwd`
  71.     fi
  72.     if [ -n "$verbose" ] ; then
  73.         echo $command
  74.     fi
  75.     eval "$command" || if [ -z "$force" ] ; then exit 1; fi
  76.  
  77.     # Collect dot file names if necessary
  78.     if [ -n "$alldirs" ] ; then
  79.         dotfiles=.*
  80.     else
  81.         dotfiles=
  82.     fi
  83.  
  84.     # For each file in current directory
  85.     for file in $dotfiles * ; do
  86.  
  87.         # Skip '.' and '..'
  88.         if [ "$file" = "." -o "$file" = ".." ] ; then
  89.         continue
  90.         fi
  91.  
  92.         # If a directory but not a symbolic link
  93.         if [ -d "$file" -a ! -h "$file" ] ; then
  94.  
  95.         # If not skipping this type of directory
  96.         if [ \( "$file" != "RCS" -a \
  97.             "$file" != "SCCS" -a \
  98.             "$file" != "CVS" -a \
  99.             "$file" != "CVS.adm" \) \
  100.             -o -z "$restricted" ] ; then
  101.  
  102.             # Recursively descend into it
  103.             $fullname $options "$command" "$file" \
  104.             || if [ -z "$force" ] ; then exit 1; fi
  105.         fi
  106.  
  107.         # Else if a directory AND a symbolic link
  108.         elif [ -d "$file" -a -h "$file" ] ; then
  109.  
  110.         if [ -z "$quiet" ] ; then
  111.             echo In directory `hostname`:`pwd`/$file: symbolic link: skipping
  112.         fi
  113.         fi
  114.     done
  115.     ) || if [ -z "$force" ] ; then exit 1; fi
  116. done
  117.