Here is my script
eval "find \\( -type f -a \\( -name '*.h' \\) \\) -print0" | xargs -0 -n100 grep -f <(echo "stdio")
echo $?
Nothing is found and the exit code is 123.
If I modify it a little as follows
echo "stdio" >.P
eval "find \\( -type f -a \\( -name '*.h' \\) \\) -print0" | xargs -0 -n100 grep <.P
echo $?
Something is found but the exit code is still 123.
Actually I just want to write a small script to make find+xargs+grep easier. For example,
xgrep -e PATTERN1 -e PATTERN2 ... *.c *.h
is to execute
find -name *.c -o -name *.h | xargs grep -f <(echo "$PATTEN1
$PATTERN2")
The use of the -f
option instead of -e
is to avoid troubles in escaping single or double quotes within the patterns.
#!/bin/bash
#set -e -o pipefail
eval ARGV=($(getopt -l '' -o 'e:li' -- "$@")) || exit 1
for((i=0;i<${#ARGV[@]};i++)) {
o="${ARGV[$i]}"
case $o in
-e)
i=$((i+1));
a="${ARGV[$i]}"
if [ -n "$grep_patterns" ]; then
grep_patterns="$grep_patterns"$'\n'
fi
grep_patterns="$grep_patterns$a"
;;
-i)
grep_options="$grep_options -i"
;;
-l)
grep_options="$grep_options -l"
;;
--)
i=$((i+1));
break;;
esac
}
for((;i<${#ARGV[@]};i++)) {
if [ -n "$find_options" ]; then
find_options="$find_options -o "
fi
find_options="${find_options}-name '${ARGV[$i]}'"
}
cmd="find \\( -type f -a \\( $find_options \\) \\) -print0"
eval "$cmd" | xargs -0 grep $grep_options -f <(echo "$grep_patterns")