Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在bash中捕获不带换行符的程序输出_Linux_Bash - Fatal编程技术网

Linux 在bash中捕获不带换行符的程序输出

Linux 在bash中捕获不带换行符的程序输出,linux,bash,Linux,Bash,我有一个程序(flash_erase),它将输出打印到标准输出,但没有换行符。这是打印进度的常用方法 例如: Erasing 4 Kibyte @ 0 -- 0 % complete 只有在流程完成后,我才能获得新的生产线: Erasing 4 Kibyte @ 1f000 -- 100 % complete 我知道如何将进度传递到另一个脚本。我可以使用tr将\r替换为\n,但仍然不起作用。输出在程序完成后进行处理 flash_erase /dev/mtd1 0 0 | tr '\r' '\

我有一个程序(flash_erase),它将输出打印到标准输出,但没有换行符。这是打印进度的常用方法

例如:

Erasing 4 Kibyte @ 0 -- 0 % complete
只有在流程完成后,我才能获得新的生产线:

Erasing 4 Kibyte @ 1f000 -- 100 % complete
我知道如何将进度传递到另一个脚本。我可以使用tr将\r替换为\n,但仍然不起作用。输出在程序完成后进行处理

flash_erase /dev/mtd1 0 0 | tr '\r' '\n' | while read line; do
    some_program $line;
done

有什么想法吗?

我找到了以下解决方案:

flash_erase /dev/mtd1 0 0 | stdbuf -i0 -o0 -e0 tr '\r' '\n' | while read -r line; do
    something $line
done

我想tr只会在换行符发出后看到整个输出。可能没有,因为在tr之后,输出被分割成单独的行。我认为问题在于tr的输出。如果您幸运的话,
stdbuf
可能会提供帮助。尝试类似于stdbuf-o0闪存擦除/dev/mtd1 0的方法。。。查看手册页了解更多选项。