Page 1 of 1
how to read input from a pipe
Posted: 02 Jul 2013 14:36
by sambasiva
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
Re: how to read input from a pipe
Posted: 02 Jul 2013 17:11
by penpen
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
Re: how to read input from a pipe
Posted: 02 Jul 2013 17:45
by Squashman
Re: how to read input from a pipe
Posted: 02 Jul 2013 18:09
by penpen
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

, 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?
Re: how to read input from a pipe
Posted: 02 Jul 2013 18:37
by penpen
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.
Re: how to read input from a pipe
Posted: 02 Jul 2013 18:48
by Squashman
If you click the Link in Jeb's thread it will take you to it.
Re: how to read input from a pipe
Posted: 03 Jul 2013 04:11
by penpen
Thanks, i haven't seen the link on first read.
Sorry for that, i should not read and post around 3:00 am.
Re: how to read input from a pipe
Posted: 04 Jul 2013 02:05
by jeb
To undestand why it works,
try a look at the definitive stream explanation
dbenham's streams.
Re: how to read input from a pipe
Posted: 05 Jul 2013 14:05
by penpen
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

Re: how to read input from a pipe
Posted: 09 Jul 2013 18:40
by Sponge Belly
Hello Sambasiva!

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

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