Florent Descroix Florent Descroix - 5 months ago 22
Linux Question

Bash 19 chars disappear with no reasons

I trying to make a batch script to compress my audio files and I face a really weird issue.

Here is my code :

#!/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


Here is a result example :

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
...


If I uncomment the
ffmpeg
line, here is the result for the same folder :

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
...


One file on two misses its 19th first chars ! And I've no idea why.

Answer

Uncommenting the ffmpeg line and running shellcheck on it reports this:

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

Try again with

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

ffmpeg, along with ssh and mplayer, are notorious for stealing stdin from while read loops.

Comments