Search code examples
bashprocess-substitutionwpa-supplicant

Fails to read lines from running process in bash


Using process substitution, we can get every lines of output of a command .

# 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)

By the same way above, I want to get the stdout output of 'wpa_supplicant' command, while discarding stderr. But nothing can be seen on screen!

while read line; do
    echo $line 
done < <(wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null)

I confirmed that typing the same command in prompt shows its output normaly.

$ wpa_supplicant -Dwext -iwlan1 -c${MY_CONFIG_FILE} 2> /dev/null

What is the mistake? Any help would be appreciated.


Solution

  • Finally I found the answer here! The problem was easy... the buffering. Using stdbuf (and piping), the original code will be modified as below.

    stdbuf -oL wpa_supplicant -iwlan1 -Dwext -c${MY_CONFIG_FILE} | while read line; do
        echo "! $line"
    done
    

    'stdbuf -oL' make the stream line buffered, so I can get every each line from the running process.