if  ! [ -t 0 ] ; then
  read var
fi

I know what the result will be but don't understand the conditional test. I've never seen it before nor can I find the -t option in any shell scripting tutorial

Recommended Answers

All 3 Replies

man test

[ ] is a "shorthand"/"shell-builtin" for the command test. And, for reference, file-handle 0 is either standard out or standard in. I forget which and it has been years since I've had to worry about it.

Ah thanks. A quick glance at the man pages shows that -t checks if the file descriptor is open and associated with a terminal.

Perhaps the '!' and the '[' should be swapped. Usually the 'not' works better if it's part of the test. :) Of course, shell scripting has become somewhat, mmm, different in the past 20 years, so it might just work the way you wrote it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.