rlm@1: #!/bin/bash rlm@1: # Copyright (c) 2004 Matthias S. Benkmann
rlm@1: # You may do everything with this code except misrepresent its origin. rlm@1: # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND! rlm@1: rlm@1: #The following list should contain the mount points of all filesystems rlm@1: #that are to be scanned as a space-separated list within parentheses. rlm@1: #/ will usually be in this list and if you have /usr rlm@1: #on a separate partition, it will also be in this list. rlm@1: #Mount points whose filesystems are special, such as procfs or sysfs must rlm@1: #not be in this list. While a simple find on those special filesystems should rlm@1: #be harmless, operations such as "-exec grep something" are NOT SAFE and may rlm@1: #have HARMFUL SIDE-EFFECTS, especially when performed as root. rlm@1: fs_to_scan=(/) rlm@1: rlm@1: #Files with a path prefix found in the following list are ignored. As the rlm@1: #main function of this script is to help you find files that contain rlm@1: #hardwired paths to /tools or other unwanted references to rlm@1: #your build system, you will usually prune any directories that don't contain rlm@1: #files of interest, such as /tools (whose files naturally refer to /tools) rlm@1: #and your package users' home directories (which may also test positive if rlm@1: #you have unpacked and configured sources lying around). rlm@1: #NOTE: The LFS-6.0 book uses a ramfs mounted on /dev and with that setup rlm@1: #/dev does not need to be in the prune list. But since there is no requirement rlm@1: #that /dev have its on filesystem it's better to prune it explicitly. rlm@1: prune_prefixes=(/home /usr/src /dev /tools) #NO TRAILING SLASHES!!! rlm@1: rlm@1: if [ $# -lt 1 -o "$1" = "--help" ]; then rlm@1: echo 1>&2 rlm@1: echo 1>&2 'USAGE: '"${0##*/}"' ' rlm@1: echo 1>&2 rlm@1: echo 1>&2 ' grep -l -- ' rlm@1: echo 1>&2 ' will be executed for each *regular file* ' rlm@1: echo 1>&2 ' ATTENTION! If you override the -l switch with a switch that makes grep' rlm@1: echo 1>&2 ' output all individual matches rather than just the matching files,' rlm@1: echo 1>&2 ' then DO NOT redirect output to a file that is in a directory that will be' rlm@1: echo 1>&2 ' scanned, or you risk creating an endless loop that will cause your' rlm@1: echo 1>&2 ' output file to grow till your disk is full.' rlm@1: echo 1>&2 rlm@1: exit 1 rlm@1: fi rlm@1: rlm@1: #suppress ugly debug output from shell rlm@1: trap ':' SIGPIPE rlm@1: rlm@1: #construct find commands that match the prune_prefixes. Each prefix will be rlm@1: #matched as -path -or -path /* rlm@1: #so that the directory itself and all subdirectories are matched. rlm@1: y=(\( -false) rlm@1: for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) rlm@1: do rlm@1: y[${#y[@]}]='-or' rlm@1: y[${#y[@]}]=-path rlm@1: y[${#y[@]}]="${prune_prefixes[$i]}" rlm@1: y[${#y[@]}]='-or' rlm@1: y[${#y[@]}]=-path rlm@1: y[${#y[@]}]="${prune_prefixes[$i]}/*" rlm@1: done rlm@1: y[${#y[@]}]=')' rlm@1: rlm@1: cmd_pre=(-type f -exec grep -l) rlm@1: cmd_post=(-- {} \;) rlm@1: rlm@1: #In the following find command, the part rlm@1: # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" ) rlm@1: #is responsible for preventing the files that match prune_prefixes from rlm@1: #being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because rlm@1: #-prune has no effect and is always false when -depth is used (which someone rlm@1: #might do in the future). rlm@1: #The -true before "$@" ensures that -depth can be passed as 1st parameter rlm@1: #of $cmd_pre (should someone change it in the future). rlm@1: find "${fs_to_scan[@]}" -xdev -noleaf \ rlm@1: -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \ rlm@1: -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)