Search code examples
bashfunctionvariableslsof

Why does lsof interpret my variables incorrectly?


I'm trying to set up a loop to monitor a file being written by Apple Compressor. Once the file is no longer being written, I'd like to change the name of the directory it's in. However, when I feed a variable containing the filepath to lsof it comes out garbled.

For instance, "/Users/leftright/Desktop/Output/${process##*/}_PROCESSING/" stored as $output is interpreted by lsof as DHt\x96?\x7f. I can't see anything in the lsof manpages to suggest why that's happening. It's being called in a if/then statement inside a function.

#!/bin/bash

compressor() {
    filepath="$1"
    process="${filepath%.*}"
    output="/Users/leftright/Desktop/Output/${process##*/}_PROCESSING/"
    filename="${filepath##*/}"
    moving="${output}""${filepath##*/}"
    cleanname="${filename%.*}"
    final= "${output}""${cleanname}".mp4
    completed="/Users/leftright/Desktop/Output/${process##*/}_COMPLETED/${cleanname}.mp4"
    mkdir -m 777 "$output";
    mv "$filepath" "$moving";
    /Applications/Compressor.app/Contents/MacOS/Compressor -computergroup "This Computer" -jobpath "$output""$filename" -settingpath /Users/leftright/Documents/CONVERTHQTEST.cmprstng -locationpath "$final";
    while true; do
        if lsof "$final" > /dev/null; then
            sleep 1
        else
            mv "$final" "$completed"
        fi
    done
}
export -f compressor
fswatch -0 -v --event Created /Users/leftright/Desktop/Watch | xargs -0 -n1 -I filepath bash -c 'compressor "filepath"'

What am I doing wrong here?


Solution

  • For those of you playing at home, I left whitespace in front of final= "${output}""${cleanname}".mp4 which caused it to be evaluated incorrectly. Cyrus' suggestion of shellcheck.net found the issue.