Detecting Pipe Using Bourne Shell
Lloyd Kremer
kremer at cs.odu.edu
Sat Apr 8 02:22:09 AEST 1989
In article: <18992 at adm.BRL.MIL> ifenn%ee.surrey.ac.uk at 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
More information about the Comp.unix.wizards
mailing list