使用WSL进行Bash进程替换



我正在努力理解我的设置中的以下(奇怪的?(行为。我正在运行带有WSL的Windows 10。

以下是我在powershell+wsl会话中看到的内容:

$ ./run.sh
Windows IP Configuration
<cut>
1

带有:

$ cat run.sh
#!/bin/bash
mkdir -p dir/1
mkdir -p dir/2
mkdir -p dir/3
var=0
while read i;
do
((var++))
ipconfig.exe
done < <(find dir -type d)
echo $var

如果我现在注释掉ipconfig.exe行:

...
#ipconfig.exe
...

现在我得到的是:

$ ./run.sh
4

为什么调用窗口可执行文件(例如ipconfig.exe(似乎会干扰使用进程替换的while循环?

供参考:

$ uname -a
Linux FR-L0002146 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ bash -version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

很可能ipconfig.exe正在从stdin读取,因此它正在吸收进程替换的输入。将其输入重定向到/dev/null以防止这种情况发生。

while read i;
do
((var++))
ipconfig.exe </dev/null
done < <(find dir -type d)

最新更新