linux - Bash 19 个字符无缘无故消失

标签 linux bash

我试图制作一个批处理脚本来压缩我的音频文件,但我遇到了一个非常奇怪的问题。

这是我的代码:

#!/bin/bash

qscale=$1
input=$2
output=$3
inputSize=$((${#input}+1))

find "$input" -type f -name "*.flac" -print0 | while read -d $'\0' a
do
    path=${a:inputSize}
    echo "Working on $path"
#    ffmpeg -i "$a" -qscale:a $qscale "$output/${path[@]/%flac/mp3}" &>/dev/null
done

这是一个结果示例:

Working on 09 - Groove #2 (instrumental studio outtake).flac
Working on 07 - Blues for Allah Sand Castles and Glass Camels Unusual Occurrences in the Desert.flac
Working on 12 - Proto 18 Proper (instrumental studio outtake).flac
Working on 04 - The Music Never Stopped.flac
Working on 13 - Hollywood Cantana (studio outtake).flac
...

如果我取消注释 ffmpeg 行,这是同一文件夹的结果:

Working on 09 - Groove #2 (instrumental studio outtake).flac
Working on h Sand Castles and Glass Camels Unusual Occurrences in the Desert.flac
Working on 12 - Proto 18 Proper (instrumental studio outtake).flac
Working on r Stopped.flac
Working on 13 - Hollywood Cantana (studio outtake).flac
...

两个文件中的一个文件错过了第 19 个第一个字符!我不知道为什么。

最佳答案

取消注释 ffmpeg 行并运行 shellcheck在它上面报告:

Line 12:
    ffmpeg -i "$a" -qscale:a $qscale "$output/${path[@]/%flac/mp3}" &>/dev/null
    ^-- SC2095: Add < /dev/null to prevent ffmpeg from swallowing stdin.

再试一次

ffmpeg -i "$a" -qscale:a $qscale "$output/${path[@]/%flac/mp3}" &>/dev/null < /dev/null

ffmpeg 以及 sshmplayer 因从 while read 循环中窃取标准输入而臭名昭著。

关于linux - Bash 19 个字符无缘无故消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777690/

相关文章:

java - 为什么我的 Java 代码无法正确执行 bash 命令?

如果文件存在,Linux 移动文件而不替换

bash "map"相当于 : run command on each file

linux - 用于注释/取消注释文件中行的 Bash 脚本

bash - 使用 & 符号在后台运行 bash 管道命令

bash - RUN 命令中字符串中的 Dockerfile ARG 替换

linux - 从一个目录执行 bash 脚本到另一个目录?

linux - 如何关闭python "sh"库的打印结果

linux - 如何卸载我检查安装的 rpm?

linux - 如果两台机器上的文件都丢失了,如何退出 shell 脚本?