I am trying to write some bash script to replace a command I quite often use. Here is the code from my file test.sh
#!/bin/bash
echo -e "\n"
i=0
args[i++]=$0
for arg in $@ ; do
args[i++]=$arg
done
where="."
what="-type f"
gcase=
str=
while getopts "d:f:F:ih" opt ; do
case $opt in
h)
echo -e "This is the help of this search function."
echo -e "\t $0 [-d <dir>] [-f|-F <pattern>] [-i] string"
echo -e "This will output the result of"
echo -e "\t find dir -[i]name pattern -exec grep --color -Hn[i] string {} \;"
echo -e "Default is"
echo -e "\t find . -type f -exec grep --color -Hn string {} \;\n"
exit 0
;;
d)
OPTIND=$(($OPTIND-1))
where=
tmp=${args[$OPTIND]}
while [[ $OPTIND -lt $# ]] && [[ "${tmp:0:1}" != "-" ]] ; do
where="$where"" $tmp"
OPTIND=$(($OPTIND+1))
tmp=${args[$OPTIND]}
done
;;
F)
what="-iname "
what="$what""\"$OPTARG\""
;;
f)
what="-name "
what="$what""\"$OPTARG\""
;;
i)
gcase="-i"
;;
\?)
echo "Invalide option, use option -h for help." >&2
exit 0
;;
esac
done
str=${args[$OPTIND]}
command="find $where $what -exec grep --color -Hn $gcase \"$str\" {} \;"
echo "$command"
$command
Now, from my terminal, I do ./test.sh -d auto-avoid -F "TEST*" "main"
and I get
find auto-avoid -iname "TEST*" -exec grep --color -Hn "main" {} \;
find: missing argument to `-exec'
(auto-avoid
is a directory with a small c++ program I wrote for fun.)
Then, in my terminal I copy-paste the command find auto-avoid -iname "TEST*" -exec grep --color -Hn "main" {} \;
and I get
auto-avoid/test.cpp:26:int main(int argc, char **argv)
which is the expected result.
The question is: what did I miss?
For now I wrote it as an independent script to test it, but the goal is to have it as a function in my .bash_aliases
.
I have found some similar topic but nothing that could help me. If you find that this is a duplicated question, I will gladly take the solution.
I am pretty sure some people will tel me to usegrep -r
, but I would at least want to understand why my script does not work. This is a minimal "not"-working example, I will exclude some directory from my find
later.