读数停止时出现管道破损错误

读数停止时出现管道破损错误

我正在尝试创建一个快速而肮脏的 bash 函数来比较两个 FLAC 文件的音频内容,而不将它们解码为临时文件。使用flac命令行工具,我将每个文件解码到命名管道中,然后将它们与cmp

function flacdiff {
        local pipe1="/tmp/$(randomString)"
        mkfifo "$pipe1" 
        flac --silent --decode --force-raw-format --sign=signed --endian=little "$1" --stdout > "$pipe1" &

        local pipe2="/tmp/$(randomString)"
        mkfifo "$pipe2"
        flac --silent --decode --force-raw-format --sign=signed --endian=little "$2" --stdout > "$pipe2" &

        cmp -b "$pipe1" "$pipe2"
        local result=$?

        rm "$pipe1"
        rm "$pipe2"

        return $result
}

如果文件相同,则脚本可以正常工作。但是,如果它们不同,我会收到损坏的管道错误:

[1]-  Broken pipe: 13         flac --silent --decode --force-raw-format --sign=signed --endian=little "$1" --stdout > "$pipe1"
[2]+  Broken pipe: 13         flac --silent --decode --force-raw-format --sign=signed --endian=little "$2" --stdout > "$pipe2"

我认为这是因为cmp在管道的写入端仍然处于活动状态时,一旦遇到第一个不匹配,就会停止读取。我的问题是这个假设是否正确,是否可以避免这些损坏的管道错误并修复我的脚本?

答案1

使用:

cmp -s \
    <(flac --silent --decode --force-raw-format --sign=signed --endian=little "$1" --stdout) \
    <(flac --silent --decode --force-raw-format --sign=signed --endian=little "$2" --stdout)

相关内容