diff previous-work/more_control_helpers/bin/grep_all_regular_files_for @ 1:d6bef198ae71

add work by Matthias S. Benkmann which is the inspiration for this project.
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Jan 2013 11:45:01 +0000
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/previous-work/more_control_helpers/bin/grep_all_regular_files_for	Tue Jan 08 11:45:01 2013 +0000
     1.3 @@ -0,0 +1,74 @@
     1.4 +#!/bin/bash
     1.5 +# Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
     1.6 +# You may do everything with this code except misrepresent its origin.
     1.7 +# PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
     1.8 +
     1.9 +#The following list should contain the mount points of all filesystems
    1.10 +#that are to be scanned as a space-separated list within parentheses. 
    1.11 +#/ will usually be in this list and if you have /usr
    1.12 +#on a separate partition, it will also be in this list. 
    1.13 +#Mount points whose filesystems are special, such as procfs or sysfs must
    1.14 +#not be in this list. While a simple find on those special filesystems should 
    1.15 +#be harmless, operations such as "-exec grep something" are NOT SAFE and may 
    1.16 +#have HARMFUL SIDE-EFFECTS, especially when performed as root. 
    1.17 +fs_to_scan=(/)
    1.18 +
    1.19 +#Files with a path prefix found in the following list are ignored. As the
    1.20 +#main function of this script is to help you find files that contain
    1.21 +#hardwired paths to /tools or other unwanted references to
    1.22 +#your build system, you will usually prune any directories that don't contain
    1.23 +#files of interest, such as /tools (whose files naturally refer to /tools)
    1.24 +#and your package users' home directories (which may also test positive if
    1.25 +#you have unpacked and configured sources lying around).
    1.26 +#NOTE: The LFS-6.0 book uses a ramfs mounted on /dev and with that setup
    1.27 +#/dev does not need to be in the prune list. But since there is no requirement
    1.28 +#that /dev have its on filesystem it's better to prune it explicitly.
    1.29 +prune_prefixes=(/home /usr/src /dev /tools) #NO TRAILING SLASHES!!!
    1.30 +
    1.31 +if [ $# -lt 1 -o "$1" = "--help" ]; then
    1.32 +  echo 1>&2 
    1.33 +  echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
    1.34 +  echo 1>&2 
    1.35 +  echo 1>&2 '  grep -l <grep-commands> -- <file>'
    1.36 +  echo 1>&2 '  will be executed for each *regular file* <file>'
    1.37 +  echo 1>&2 '  ATTENTION! If you override the -l switch with a switch that makes grep'
    1.38 +  echo 1>&2 '  output all individual matches rather than just the matching files,'
    1.39 +  echo 1>&2 '  then DO NOT redirect output to a file that is in a directory that will be'
    1.40 +  echo 1>&2 '  scanned, or you risk creating an endless loop that will cause your'
    1.41 +  echo 1>&2 '  output file to grow till your disk is full.'
    1.42 +  echo 1>&2 
    1.43 +  exit 1
    1.44 +fi
    1.45 +
    1.46 +#suppress ugly debug output from shell
    1.47 +trap ':' SIGPIPE
    1.48 +
    1.49 +#construct find commands that match the prune_prefixes. Each prefix will be
    1.50 +#matched as -path <prefix> -or -path <prefix>/*
    1.51 +#so that the directory itself and all subdirectories are matched.
    1.52 +y=(\( -false)
    1.53 +for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) 
    1.54 +do
    1.55 +  y[${#y[@]}]='-or'
    1.56 +  y[${#y[@]}]=-path
    1.57 +  y[${#y[@]}]="${prune_prefixes[$i]}"
    1.58 +  y[${#y[@]}]='-or'
    1.59 +  y[${#y[@]}]=-path
    1.60 +  y[${#y[@]}]="${prune_prefixes[$i]}/*"
    1.61 +done
    1.62 +y[${#y[@]}]=')'
    1.63 +
    1.64 +cmd_pre=(-type f -exec grep -l)
    1.65 +cmd_post=(-- {} \;)
    1.66 +
    1.67 +#In the following find command, the part
    1.68 +# -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
    1.69 +#is responsible for preventing the files that match prune_prefixes from
    1.70 +#being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
    1.71 +#-prune has no effect and is always false when -depth is used (which someone
    1.72 +#might do in the future).
    1.73 +#The -true before "$@" ensures that -depth can be passed as 1st parameter
    1.74 +#of $cmd_pre (should someone change it in the future).
    1.75 +find "${fs_to_scan[@]}" -xdev -noleaf \
    1.76 +     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
    1.77 +     -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)