Skip to content

Instantly share code, notes, and snippets.

@kristjan
Created February 1, 2011 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristjan/805168 to your computer and use it in GitHub Desktop.
Save kristjan/805168 to your computer and use it in GitHub Desktop.
A little something to prevent hanging greps
#!/usr/bin/env ruby
query = $*.shift
if $*.empty? && $stdin.tty?
puts "You haven't given grep anything"
else
cmd = %(grep "#{query}" #{$*.join(' ')})
puts `#{cmd}`
end
@jneen
Copy link

jneen commented Mar 10, 2011

Nice! Probably more portable in bash, I think:

function fancy-grep() {
  local query="$1"; shift
  if [[ -z "$@" ]] && [[ -t 0 ]]; then
    echo "You haven't given grep anything." >&2
  else
    /bin/grep "$query" "$@"
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment