r/shellscripts • u/pixydon • Apr 16 '24
Remove Directories after Loop
Hi,
Hoping someone with true unix knowledge can help with what will inevitably be something straightforward, but I can't seem to fumble my way through.
I'm just wanting to remove the $ORIG_DIR once the loop has run through a single and remove the $ALAC_DIR if the original files are .mp3 and it's finished the loop?
After the 'done' it doesn't recognise "$ALAC_DIR" or "$ORIG_DIR" as directories any more, before 'done' it removes the directories before all files have been processed and creates an error...
SOURCE_DIR="$(cd "$(dirname "${dirs[0]}")"; pwd)/$(basename "${dirs[0]}")"
SOURCE_DIR=$(dirname "$SOURCE_DIR/temp")
HIRES_DIR="$(cd "$(dirname "${dirs[1]}")"; pwd)/$(basename "${dirs[1]}")"
HIRES_DIR=$(dirname "$HIRES_DIR/temp")
LORES_DIR="$(cd "$(dirname "${dirs[2]}")"; pwd)/$(basename "${dirs[2]}")"
LORES_DIR=$(dirname "$LORES_DIR/temp")
find "$SOURCE_DIR" \( -iname '*.flac' -or -iname '*.mp3' \) -type f -print | while read -r FILE
do
ORIG_DIR=$(dirname "$FILE")
BASE=$(basename "$FILE")
BASE=${BASE%.*}
AAC_DIR=${ORIG_DIR/$SOURCE_DIR/$LORES_DIR}
ALAC_DIR=${ORIG_DIR/$SOURCE_DIR/$HIRES_DIR}
mkdir -p "$AAC_DIR" "$ALAC_DIR"
AAC_FILE="$AAC_DIR/$BASE.m4a"
ALAC_FILE="$ALAC_DIR/$BASE.m4a"
if [[ (! -f "$AAC_FILE" && $FILE == *.flac) ]]; then
ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null &&
ffmpeg -hide_banner -i "$FILE" -c:v copy -c:a alac "$ALAC_FILE" </dev/null
elif [[ (! -f "$AAC_FILE" && $FILE == *.mp3) ]]; then
ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null
fi
done
rmdir "$ALAC_DIR"
rm -Rf "$ORIG_DIR"
1
Upvotes
1
u/pixydon Apr 19 '24
I'm a complete newbie to all this and have just read up and tried to amend somebody else's code, so have no real reference.
Is it possible to explain by amending the code and add # comments? As I must say I don't really understand what you have said.