Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!pasteur!ames!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.unix.wizards Subject: Re: Detecting Pipe Using Bourne Shell Summary: Try this... Message-ID: <8385@xanth.cs.odu.edu> Date: 7 Apr 89 16:22:09 GMT References: <18992@adm.BRL.MIL> Organization: Old Dominion University, Norfolk, Va. Lines: 81 In article: <18992@adm.BRL.MIL> ifenn%ee.surrey.ac.uk@nss.cs.ucl.ac.uk (Ian Fenn) writes: >If I enter the program with >no arguments: >I display a main menu which offers options to change entries, >delete entries, etc, etc. If I enter the program with arguments >Then it searches through the database (using grep) for the arguments >using: >if test $# -ne >then .....search for arguments in database..... > .....then exit >fi >....rest of program (i.e. Main Menu). >The only problem with this is that I cannot pipe the output from another >program into it because it drops into the main menu and out again! >% cat datafile | phone You are trying to interpret "no arguments" as two entirely different directives: a) present menu and exit and b) read stdin instead of argument list for search targets You must devise a way of differentiating between these meanings. Many UNIX(tm) programs treat an argument of - as meaning 'read stdin instead of files'. How about something like this: #!/bin/sh # phone lookup utility if [ $# = 0 ] then present menu elif [ $1 = - ] then while read $i do grep "$i" database done else for i in $* do grep "$i" database done fi exit 0 Or, to be a bit more elegant: #!/bin/sh # phone lookup utility if [ $# = 0 ] then present menu else if [ $1 = - ] then set `while read i;do echo "$i";done` fi for i in $* do grep "$i" database done fi exit 0 Either of these should allow some_program | phone - to work properly. Hope this helps, Lloyd Kremer {uunet,sun,...}!xanth!kremer