Open a programming cookbook for any scripting language and you'll find "how do I tell if I'm reading from a pipe or from the console?" In ruby I mostly ignore this by reading from ARGF, but recently I needed this in plain old bash. Turns out it exists: The -t test, check it out: https://github.com/dbrady/scrapbin/blob/master/bash/pipe-or-terminal

So then I went back to ruby and thought "man, if I ran the zoo I would make this a convenience method on IO." Oh, wait. THEY DID.

$stdin.tty?

I love it when ruby's "I love you" sees me coming.

scrapbin/pipe-or-terminal at master · dbrady/scrapbin

Random code fragments and tools. Contribute to dbrady/scrapbin development by creating an account on GitHub.

GitHub