I use this bash command often
find ~ -type f -name \*.smt -exec grep something {} /dev/null \;
so I am trying to turn it into a simple bash script that I would invoke like this
findgrep ~ something --mtime -12 --name \*.smt
Thanks to this answer I managed to make it work like this:
if ! options=$(getopt -o abc: -l name:,blong,mtime: -- "$@")
then
exit 1
fi
eval "set -- $options"
while [ $# -gt 0 ]
do
case $1 in
-t|--mtime) mtime=${2} ; shift;;
-n|--name|--iname) name="$2" ; shift;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
if [ $# -eq 2 ]
then
dir="$1"
str="$2"
elif [ $# -eq 1 ]
then
dir="."
str="$1"
else
echo "Need a search string"
exit
fi
echo "find $dir -type f -mtime $mtime -name $name -exec grep -iln \"$str\" {} /dev/null \;"
echo "find $dir -type f -mtime $mtime -name $name -exec grep -iln \"$str\" {} /dev/null \;" | bash
but the last line - echo'ing a command into bash - seems outright barbaric, but it works.
Is there a better way to do that? somehow trying to execute the find command directly gives no output, while running the one echo'ed out in bash works ok.
... -name "$name" ...instead of just... -name $name ...). See "When should I wrap quotes around a shell variable?" (short answer: almost always).find $dir -type f -mtime $mtime -name "$name" -exec grep -iln \"$str\" {} /dev/null \;fails to produce any output. Keep in mind that$nameis a string with globs like"\*pl"- I'm guessing that's the reason, but I can't work around that$str(i.e. use... grep -iln "$str" ...instead of... grep -iln \"$str\" ...) -- with the escapes, those quotes are treated as part of the pattern to search for, instead of as protecting that pattern from shell interpretation.$namevariable like this:find $dir -type f -iname \*pl -mtime $mtime -exec grep -in "$str" {} /dev/null \;. But that's one of the parameters I want to be able to set.