how to read input from a pipe

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

how to read input from a pipe

#1 Post by sambasiva » 02 Jul 2013 14:36

Hi All,

Requirement 1:
--------------
Is there a way to find out whether the stdin that we get from a pipe is empty or not ?
exmple: echo some_string | test.bat

Here in test.bat I want to find out whether the stdin (some_string in this case) that I get from the pipe is empty or not.
Then set some flag if it is empty.

Requirement 2:
--------------
Also if the test.bat is executed simply as test.bat without any pipes then I don't need to check for the standard input.


I need to write a batch script which meets both these requirements in a single batch file.

Please help.

Thanks
Sambasiva

penpen
Expert
Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to read input from a pipe

#2 Post by penpen » 02 Jul 2013 17:11

Ad 1)
You cannot check, if data is in the pipe without reading it, when using batch commands only.
You may write a program in C++ to check this and call this self created executeable from within the batch program.
I am not firm enough with JScript and VBS to know if that is possible with these languages. The advantage of this approach, if possible, is that this
script can be called from nearly all windows pcs (if cscript.exe has not been disabled for executing) without installing additional executables.

Ad 2)
Piping data to a program, and also redirecting inputs from or to this program, is transparent for this program.
So you cannot check if data is piped, or redirected.

penpen


penpen
Expert
Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to read input from a pipe

#4 Post by penpen » 02 Jul 2013 18:09

Squashman wrote:https://groups.google.com/forum/m/#!top ... dX14-F_Aks
Sry I cannot read this, as i have no account there. I only get an loginscreen.

But the detection of piped and redirected input is quite cool :P , i never learn out.
So, normally this is transparent to a program, as it normally doesn't affect any other input/output handles... .
I wonder what microsoft does, when redirecting /piping is performed.
Does it save the old input/output stream handles, what if all streams are initially in use before creating a pipe or redirection?

penpen
Expert
Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to read input from a pipe

#5 Post by penpen » 02 Jul 2013 18:37

I have just tested the batch from the second link Squashman has given.
The result, when naming this batch redir.bat and creating a non empty test.txt, is as follows

Code: Select all

Z:\Temp\batch>echo:something | redir.bat 3< test.txt 4< test.txt 5< test.txt 6<
test.txt 7< test.txt 8< test.txt 9< test.txt
Input type is REDIRECTION

Z:\Temp\batch>echo:something | redir.bat 3< test.txt

Z:\Temp\batch>test.txt > redir.bat 3< test.txt
Zugriff verweigert
Zugriff verweigert
Zugriff verweigert
Zugriff verweigert

Additionally when executing the third command WinXP home SP 3 opens a notepad window with the content of test.txt.
The third command is neverending, waiting for return. to show an additional "Zugriff verweigert" == "Access denied."

Sad to say this batch is not completely save.

Squashman
Expert
Posts: 4198
Joined: 23 Dec 2011 13:59

Re: how to read input from a pipe

#6 Post by Squashman » 02 Jul 2013 18:48

If you click the Link in Jeb's thread it will take you to it.

penpen
Expert
Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to read input from a pipe

#7 Post by penpen » 03 Jul 2013 04:11

Thanks, i haven't seen the link on first read.
Sorry for that, i should not read and post around 3:00 am.

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to read input from a pipe

#8 Post by jeb » 04 Jul 2013 02:05

To undestand why it works,
try a look at the definitive stream explanation dbenham's streams.

penpen
Expert
Posts: 1864
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to read input from a pipe

#9 Post by penpen » 05 Jul 2013 14:05

Clever analysis.

So my intuitive assumtion that leads to my first questions above was right: MS saves the old handles to the prior unused ones.
And the answer on my other question then is in short:"Access denied".

Good to know this now (and not only assume it).

Thanks :D

Sponge Belly
Posts: 200
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: how to read input from a pipe

#10 Post by Sponge Belly » 09 Jul 2013 18:40

Hello Sambasiva! :-)

I recommend the find or findstr prompt issue topic. See my contribution at the end. :D

If you have any further questions, don’t hesitate to ask.

- SB

Post Reply