Skip to content

Instantly share code, notes, and snippets.

@jesboat
Created July 6, 2013 03:19
Show Gist options
  • Save jesboat/5938492 to your computer and use it in GitHub Desktop.
Save jesboat/5938492 to your computer and use it in GitHub Desktop.
fgrep in sh (maybe bashisms)
fgrep() {
# Parse options
local dashv=0 dashq=0 dashx=0 opt= OPTIND=1
local usage='
echo "Usage: fgrep [-vqx] string" >&2;
return 1;
'
while getopts "vqx" opt; do
case "$opt" in
'v') dashv=1 ;;
'q') dashq=1 ;;
'x') dashx=1 ;;
'?') eval "$usage" ;;
esac
done
shift $(( OPTIND - 1 ))
if [ $# != 1 ]; then
eval "$usage"
fi
# Do matching
local line= matched= found=0
while read line; do
case "$line" in
"$1") let 'matched = !dashv' ;;
*"$1"*) let 'matched = (dashx ? dashv : !dashv)' ;;
*) let 'matched = dashv' ;;
esac
if [ $matched = 1 ]; then
found=1
if [ $dashq = 1 ]; then
break
else
echo "$line"
fi
fi
done
return $(( ! found ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment