So, i mixed up things in previous post. After reading
viewtopic.php?t=6610&start=15#p43422, i could understand it better but still have some suspicions.
This one is fine.
Code: Select all
(
echo Output1
echo Output2 >&2
echo Output3 >&3
echo Output4 >&4
echo Output5 >&5
) 1>&2 2>&3 | (echo # Running findstr & findstr /A:4E /N "^")
### Result
Output1
Output4
Output5
# Running findstr
1:Output2
2:Output3
- 1>&2
- 2 need to be duplicated to 1 but 1 is not empty.
- dup(1)->3(currently empty one), 3=stdout
- close(1), 1=nothing
- dup2(2,1), 1=stderr(2 contains stderr so its duplicated to 1)
- 2>&3
- 3 need to be duplicated to 2 but 2 is not empty.
- dup(2)->4(currently empty one), 4=stderr
- close(2)
- dup2(3, 2), 2=3(3 has stdout)
- Finally 1=stderr, 2=3=stdout, 4=stderr
But for this one, i could not find out how handle 5 is redirected?
Code: Select all
(
echo Output1
echo Output2 >&2
echo Output3 >&3
echo Output4 >&4
echo Output5 >&5
) 1>&3 2>&3 | (echo # Running findstr & findstr /A:4E /N "^")
### Result
Output4
# Running findstr
1:Output1
2:Output2
3:Output3
4:Output5
# Interpretation 1(3 is assumed to be undefined)
- 1>&3
- 3 need to be duplicated to 1 but 1 is not empty.
- dup(1)->3(currently empty one), 3=stdout
- close(1), 1=nothing
- dup2(3,1), 1=3(3 is 3)
- 2>&3
- 3 need to be duplicated to 2 but 2 is not empty.
- dup(2)->4(currently empty one), 4=stderr
- close(2)
- dup2(3, 2), 2=3(3 is 3)
- Finally 1=2=3=stdout, 4=stderr, what about 5?
# Interpretation 2(when 3 is assumed to be used or not available because it is used in this redirection);
- 1>&3
- 3 need to be duplicated to 1 but 1 is not empty.
- dup(1)->4(currently empty one), 4=stdout
- close(1), 1=nothing
- dup2(3,1), 1=3(3 is 3)
- 2>&3
- 3 need to be duplicated to 2 but 2 is not empty.
- dup(2)->5(currently empty one), 5=stderr
- close(2)
- dup2(3, 2), 2=3(3 is 3)
- Finally 1=2=3=stdout, 4=stdout, 5=stderr but 5 must be stdout and 4 must not be stdout!
2>&3 is another riddle
Solving it will solve the previous one i think.
Code: Select all
(
echo Output1
echo Output2 >&2
echo Output3 >&3
echo Output4 >&4
) 2>&3 | (echo # Running findstr & findstr /A:4E /N "^")
### Result
Output2
Output3
# Running findstr
1:Output1
2:Output4
# Interpretation 1
- 2>&3
- 3 need to be duplicated to 2 but 2 is not empty.
- dup(2)->4(currently empty one), 4=stderr
- close(2), 2=nothing
- dup2(3,2), 2=3(3 is 3)
- Finally, 1=stdout, 2=3=stdout, 4=stderr ??
# Interpretation 2
- 2>&3
- 3 need to be duplicated to 2 but 2 is not empty.
- dup(2)->3(currently empty one), 3=stderr
- close(2), 2=nothing
- dup2(3,2), 2=3(3 is stderr)
- Finally, 1=stdout, 2=3=stderr, 4=?
- According to Aacini (
https://stackoverflow.com/a/9888382/8227155)
Handle 3 is connected to CON: device (keyboard for input, screen for output); handles 4-9 does not.
I also think, handle 3 is connected to stdin/stdout and it seems to be correct.
- But according to dbenham(
https://stackoverflow.com/a/9880156/8227155) handle 3 is undefined
It also explains Aacini's observation that handle 3 appears to be connected to CON: (It isn't, it is actually undefined as per Windows documentation).
- So, while interpreting redirection algorithm should we assume handle 3 is undefined or stdin/stdout?
And in either case, we couldn't successfully interpret how above two samples work! Although for the first one assuming it as undefined worked fine.