#!/bin/bash usage() { echo 'usage:' echo " `basename $0` [options] [path...] pattern" echo " `basename $0` [options] -f FILE [path...]" echo echo 'options:' echo ' -user uname - File is owned by user uname.' echo ' -group gname - File belongs to group gname.' echo ' -maxdepth n - Descend at most n levels of directories' echo ' -f FILE - Obtain patterns from FILE, one per line' echo ' -i - Ignore case distinctions' echo ' -v - Invert the sense of matching.' } # Pokud se jedna o posledni parametr a grep # nema parametr -f nastavi PATTERN, jinak prida # parametr do seznamu cest read_path() { if [ $2 -eq 1 -a $PATFILE -eq 0 ] ; then PATTERN="$1" else if [ -d "$1" ] ; then PATHS[$DIR_NUM]="$1" DIR_NUM=$((DIR_NUM+1)) else echo "$1: no such directory" exit 1 fi fi } # Grep a "inverzni" grep s pozadovanymy parametry # nad danym souborem grep_file() { if [ $INVERT -eq 1 ] ; then if ! grep $GREPOPT "$PATTERN" "$1" ; then echo "`pwd`/$1" fi else if grep $GREPOPT "$PATTERN" "$1" ; then echo "`pwd`/$1" fi fi } # Vlastni vyhledavaci funkce. Rekurzivne # prochazi aktualni adresar search() { for i in * ; do if [ -d "$i" -a -x "$i" ] && ! [ -L "$i" ] ; then if [ $1 -lt $MAXDEPTH ] ; then cd "$i" DEPTH=$(($1+1)) search $DEPTH DEPTH=$(($1-1)) cd .. fi else if [ -f "$i" ] ; then GREP=1 if [ $USR ] ; then FO=`ls -l "$i" | awk '{print $3}'` if [ "$USR" != "$FO" ] ; then GREP=0 fi fi if [ $GRP ] ; then FG=`ls -l "$i" | awk '{print $4}'` if [ "$GRP" != "$FG" ] ; then GREP=0 fi fi if [ $GREP -eq 1 ] ; then grep_file "$i" fi fi fi done } # Rozparsovani vstupu if [ $# -lt 1 ] ; then usage exit 1 fi PARA=1; DIR_NUM=0; INVERT=0; PATFILE=0 while [ $# -ne 0 ] ; do case "$1" in -user) if [ $PARA -eq 0 -o $# -lt 2 ] ; then read_path "$1" $# shift else USR="$2" shift 2 fi ;; -group) if [ $PARA -eq 0 -o $# -lt 2 ] ; then read_path $1 $# shift else GRP="$2" shift 2 fi ;; -maxdepth) if [ $PARA -eq 0 -o $# -lt 2 ] ; then read_path "$1" $# shift else if ! [ $2 -gt 0 -o $2 -le 0 ] 2>/dev/null ; then echo "invalid maxdepth value" exit 1 fi MAXDEPTH=$2 shift 2 fi ;; -f) if [ $PARA -eq 0 -o $# -lt 2 ] ; then read_path "$1" $# shift else if [ ! -f "$2" ] ; then echo "$2: no such pattern file" exit 1 fi PATTERN="`pwd`/$2" PATFILE=1 shift 2 fi ;; -i) if [ $PARA -eq 0 ] ; then read_path "$1" $# else GREPOPT="$GREPOPT -i" fi shift ;; -v) if [ $PARA -eq 0 ] ; then read_path "$1" $# else INVERT=1 fi shift ;; -h|-help) if [ $PARA -eq 0 ] ; then read_path "$1" $# shift else usage exit 1 fi ;; *) read_path "$1" $# PARA=0 shift ;; esac done # implicitni cesta je ./ if [ $DIR_NUM -eq 0 ] ; then PATHS[0]='./' DIR_NUM=1 fi # implicitni hloubka prohledavani # je nekonecno (skoro...) if [ ! $MAXDEPTH ] ; then MAXDEPTH=1024 fi # implicitni parametry grepu if [ $PATFILE -eq 1 ] ; then GREPOPT="$GREPOPT -q -f" else GREPOPT="$GREPOPT -q -e" fi # Vyhledavani v zadanych adresarich PWD=`pwd` DIR_NUM=$((DIR_NUM-1)) while [ $DIR_NUM -ge 0 ] ; do cd "${PATHS[$DIR_NUM]}" search 1 DIR_NUM=$((DIR_NUM-1)) cd "$PWD" done