#!/bin/bash
set -o noglob
usage()
{
echo "Usage: search [-e] [-f] regex [files] [directory]"
echo " -e: use extended regular expression"
echo " -f: display full path"
echo " default files to find is /"*/""
echo " default starting directory is /"./""
}
CMD_FILE=/tmp/.searh.$USER.cmd
OUTPUT_FILE=/tmp/.search.$USER.log
flag_egrep=
flag_fullpath=
cmdline=$(basename $0)" $@"
args=$(getopt -q -u -o "efh" -- "$@")
set --$args
for i
do
case "$i"
in
-e)
flag_egrep='-E';
shift;;
-f)
flag_fullpath='y';
shift;;
-o)
echo oarg is "'"$2"'"; oarg="$2"; shift;
shift;;
-h)
usage;
exit 0;;
--)
shift; break;;
esac
done
REG_EXP=$1
FILES=$2
START_DIR=$3
## display the lastest searching result
if [ -z "$REG_EXP" ] ; then
echo -ne "Excuting "
cat $CMD_FILE 2>/dev/null
cat $OUTPUT_FILE 2>/dev/null
exit 0
fi
echo "$cmdline" >$CMD_FILE
[ -z "$START_DIR" ] && START_DIR='.'
if [ "$flag_fullpath" = "y" ] ; then
START_DIR=$(cd $START_DIR; pwd)
fi
[ -z "$FILES" ] && FILES="/.?.*"
find "$START_DIR" -regex "$START_DIR/$FILES" -xdev -type f -follow -exec grep $flag_egrep -I -l "$REG_EXP" {} /; 2>/dev/null | tee $OUTPUT_FILE
exit $?