I am trying to add recursive support to my filegrep script.
For some reason this does not work as expected:
RECURSIVE_MODE=off # iterate over args for ARG in "$@" do if [ -d "$ARG" ] && [ $RECURSIVE_MODE = on ] ; then # recursive call $0 $GREP_ARGS $ARG/* elif [ -f "$ARG" ]; then filecat "$ARG" | grep $GREP_ARGS | sed "s#^#$ARG: #" else [ "$ARG" = "-r" ] && RECURSIVE_MODE=on # append to the current grep args GREP_ARGS="$GREP_ARGS $ARG" fi done
I'd like to avoid using functions in this case for better readability.
EDIT: the script should work like this:
for each passed arg:
- if the current arg is a directory, check if recursive mode is enabled and if yes do the recursive call;
- else if the current arg is a file, call
grepwith the current pattern;
- else assume the current arg is a grep pattern or a grep switch and append it to the current
Maybe this is not completely reliable, but it works fine for me as a replacement for