无法从 bash 中的 运行 进程读取行
Fails to read lines from running process in bash
使用进程替换,我们可以获得命令输出的每一行。
# Echoes every seconds using process substitution
while read line; do
echo $line
done < <(for i in $(seq 1 10); do echo $i && sleep 1; done)
通过与上面相同的方式,我想获得 'wpa_supplicant' 命令的 stdout 输出,同时丢弃 stderr。
但是屏幕上什么也看不到!
while read line; do
echo $line
done < <(wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null)
我确认在提示符下输入相同的命令可以正常显示其输出。
$ wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null
错误是什么?任何帮助将不胜感激。
终于找到答案了here!
问题很简单……缓冲。使用stdbuf(和管道),原始代码将修改如下。
stdbuf -oL wpa_supplicant -iwlan1 -Dwext -c${MY_CONFIG_FILE} | while read line; do
echo "! $line"
done
'stdbuf -oL' 使流线缓冲,因此我可以从 运行 进程中获取每一行。
使用进程替换,我们可以获得命令输出的每一行。
# Echoes every seconds using process substitution
while read line; do
echo $line
done < <(for i in $(seq 1 10); do echo $i && sleep 1; done)
通过与上面相同的方式,我想获得 'wpa_supplicant' 命令的 stdout 输出,同时丢弃 stderr。 但是屏幕上什么也看不到!
while read line; do
echo $line
done < <(wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null)
我确认在提示符下输入相同的命令可以正常显示其输出。
$ wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null
错误是什么?任何帮助将不胜感激。
终于找到答案了here! 问题很简单……缓冲。使用stdbuf(和管道),原始代码将修改如下。
stdbuf -oL wpa_supplicant -iwlan1 -Dwext -c${MY_CONFIG_FILE} | while read line; do
echo "! $line"
done
'stdbuf -oL' 使流线缓冲,因此我可以从 运行 进程中获取每一行。