如何通过管道 tail -f 到 iconv 命令?

rog*_*ger 4 encoding pipe tail iconv

我有一个使用 gbk 编码的日志文件,我必须像这样读取数据:

tail -n 2000 nohup.out | iconv -f gbk -t utf-8
Run Code Online (Sandbox Code Playgroud)

但是当我使用tail -f它时不会打印任何内容:

tail -f nohup.out | iconv -f gbk -t utf-8
Run Code Online (Sandbox Code Playgroud)

Rog*_*tor 6

在类似的情况下,我使用一个脚本来读取每一行并进行转换。在您的情况下: tail -f nohup.out | 图标文件

#!/bin/bash
#iconv.sh
IFS=''
while read line
do 
    echo "$line" | iconv -f gbk -t utf-8
done  < "${1:-/dev/stdin}"
Run Code Online (Sandbox Code Playgroud)