How do I detect if input is from a pipe or not?

B

badbob

I'm writing a utility that can get input from a file or from a pipe'd
output from another program:

myprogram -infile test.txt

or

type test.txt | myprogram

So for the above two scenarios, I have:

if ($opt_infile)
{
# read from file
}
else
{
while (<>)
{
#read from stdin
}
}

The problem is, if I just run

myprogram

it sits there waiting for input. What I really want is for it to
detect that nothing is coming in from a PIPE so it should complain
that the program was run incorrectly. I've searched and I've seen
stuff on named pipes and getting output by calling a specific program,
but I need a way to get input from any program.

So what I really want is:

if ($opt_infile)
{
# read from file
}
elsif ( pipe_detected() )
{
while (<>)
{
#read from stdin
}
}
else
{
showUsage();
}

Is this possible? Thanks!
badbob001 (at yahoo period com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top