This browser gist by defunkt github user starts with this shell expression
if [ -t 0 ]; then ...
What is the meaning of this line of code?
UPDATE: could you also explain why i need this check before doing anything else?
For the sake of completeness, here is the entire little script (it allow to pipe text to the default browser):
if [ -t 0 ]; then
if [ -n "$1" ]; then
open $1
else
cat <<usage
Usage: browser
pipe html to a browser
$ echo '<h1>hi mom!</h1>' | browser
$ ron -5 man/rip.5.ron | browser
usage
fi
else
f="/tmp/browser.$RANDOM.html"
cat /dev/stdin > $f
open $f
fi
[and]invokestest-tmakes thetesttest a file descriptor to see if it’s a terminal0is the file descriptor for STDIN.So that says
Motivation
I’d have to read the whole script to know for sure, but usually it’s because the script wants to do something visually slick like clear the screen, or prompt interactively. If you’re reading a pipe, there’s no point in doing that.
Detail
Okay, let’s examine the whole script:
So this is checking to see if you’re on a terminal; if so it looks for an argument with a file name or URL. If it isn’t a terminal, then it tries to display the input as html.