Path: utzoo!attcan!uunet!cs.utexas.edu!rutgers!bpa!dsinc!lgnp1!rbarrell From: rbarrell@lgnp1.LS.COM (Robert Barrell) Newsgroups: comp.unix.questions Subject: How to tell if you're in a pipe... Keywords: pipe tty(1) sh(1) isatty(4) Message-ID: <1798@lgnp1.LS.COM> Date: 22 Jun 89 11:55:53 GMT Organization: Lagniappe Systems [Norristown PA] Lines: 28 [The machine through which I usually access the newsgroups had a bit of a burp, so please forgive me if I'm repeating an answer, here.] I don't know if it was ever answered, or not, but someone had asked about how to determine if the program they were running was being used directly, or in a pipe. In glancing through the manuals, I happened to notice the isatty() C call, and what it does, which prompted me to examine the "tty" shell command. In doing so, I found that "tty" returns a 0 if standard-input is a terminal, and returns a 1 if it isn't. Also, with the "-s" option, "tty" can be made silent, so the exit code may be used without having to redirect the output of "tty" to /dev/null. Thus, the following script will tell you if it is being run in a pipe or not: if [ `tty -s;echo $?` = 0 ] then echo "terminal" else echo "pipe" fi I hope this isn't to late (or already redundant) to be of use to the person who originally asked the question. ------------------------------------------------------------------------------ Robert Barrell "Hodgepodge" - Sysop of Milo's Meadow - A Citadel/UX BBS 215-323-0497 - 300/1200/2400 - 24hrs. - login as "bbs" ------------------------------------------------------------------------------