Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
einstein1969
- Expert
- Posts: 961
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
#1
Post
by einstein1969 » 31 Jan 2016 12:37
Hi,
Why this code execute in different mode?
code1:
Code: Select all
@echo off
( for /L %%N in (0) do (
ping -n 2 127.0.0.1 >nul
start "HBPING" /B ping -n 23 127.0.0.1
)
) | find /N "128"
code2:
Code: Select all
@echo off
for /L %%N in (0) do (
ping -n 2 127.0.0.1 >nul
start "HBPING" /B ping -n 23 127.0.0.1
) | find /N "128"
thanks
Einstien1969
Last edited by
einstein1969 on 02 Feb 2016 05:43, edited 1 time in total.
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#2
Post
by dbenham » 31 Jan 2016 15:35
In your first code, the entire FOR output is captured in an outer set of parentheses, and this is piped to FIND. So FIND is only executed once.
In the second code, the output of each iteration is piped to FIND. So FIND is executed once for each iteration.
I think it is easier to follow with simplified tests:
test1.bat
Code: Select all
@echo off
(
for /L %%N in (1 1 5) do @(
echo %%N-A
echo %%N-B
)
) | find /n /v ""
--OUTPUT--
Code: Select all
[1]1-A
[2]1-B
[3]2-A
[4]2-B
[5]3-A
[6]3-B
[7]4-A
[8]4-B
[9]5-A
[10]5-B
test2.bat
Code: Select all
@echo off
for /L %%N in (1 1 5) do (
echo %%N-A
echo %%N-B
) | find /n /v ""
--OUTPUT--
Code: Select all
[1]1-A
[2]1-B
[1]2-A
[2]2-B
[1]3-A
[2]3-B
[1]4-A
[2]4-B
[1]5-A
[2]5-B
Dave Benham
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#3
Post
by dbenham » 31 Jan 2016 16:05
More variation on the theme
Test1 and Test2 are alternate ways of accomplishing the same things as test1.bat and test2.bat.
Test3 is an entirely different scenario. It demonstrates that a | has higher precedence than &.
Code: Select all
@echo off
echo(
echo TEST1
echo ------------
(for /L %%N in (1 1 5) do @echo %%N-A & echo %%N-B) | find /n /v ""
echo(
echo TEST2
echo ------------
for /L %%N in (1 1 5) do (echo %%N-A & echo %%N-B) | find /n /v ""
echo(
echo TEST3
echo ------------
for /L %%N in (1 1 5) do echo %%N-A & echo %%N-B | find /n /v ""
--OUTPUT--
Code: Select all
TEST1
------------
[1]1-A
[2]1-B
[3]2-A
[4]2-B
[5]3-A
[6]3-B
[7]4-A
[8]4-B
[9]5-A
[10]5-B
TEST2
------------
[1]1-A
[2]1-B
[1]2-A
[2]2-B
[1]3-A
[2]3-B
[1]4-A
[2]4-B
[1]5-A
[2]5-B
TEST3
------------
1-A
[1]1-B
2-A
[1]2-B
3-A
[1]3-B
4-A
[1]4-B
5-A
[1]5-B
Dave Benham
-
einstein1969
- Expert
- Posts: 961
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
#4
Post
by einstein1969 » 01 Feb 2016 09:03
thanks Dave.
but i knew that for a pipe "|" there is a CMD child that execute. It is possible to see with %cmdcmdline% the left part?
On the variation theme seems that test1 execute like:
Code: Select all
( for /L %%N in (1 1 5) do (@echo %%N-A & echo %%N-B) ) | find /n /v ""
for the test2 that with the FOR is not the case (the CMD child there isn't). It's right?
And seems that the test3 is like:
Code: Select all
for /L %%N in (1 1 5) do echo %%N-A & ( echo %%N-B | find /n /v "" )
without CMD child in the FOR part. It's right?
einstein1969