Magic Lantern Forum

Using Magic Lantern => Post-processing Workflow => Topic started by: timbytheriver on October 29, 2021, 12:47:19 PM

Title: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 12:47:19 PM
Hi

I'm trying to run a command on Mac OS in Terminal to batch modify a folder of MLV files to rewrite their WhiteLevel.

I have a [working on PC] Powershell command that I'm attempting to rewrite into bash that is not working on Mac so far. When I run it, the terminal beeps, and the screen is filled with a flood of random characters until I stop it. The mlvs remain unmodified.

Can someone help please?

Powershell (working)

$FILES = Get-ChildItem  .\\* -recurse -Include  *.mlv
foreach ($file in $FILES) {
  echo "Processing $file..."
  echo "0000068: FF 3F" | xxd -r - $file
}


Bash (not working)

#!/bin/bash
FILES="/Users/me/Desktop/media/*.mlv"
for f in $FILES
do
  echo "0000068: FF 3F" | xxd -r - $file
  cat "$f"
done

Many thanks.
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: names_are_hard on October 29, 2021, 02:25:26 PM
In the Mac attempt, you are using a variable uninitialised, here:
Quoteecho "0000068: FF 3F" | xxd -r - $file

Replace $file with $f.

Bash will happily replace variables that don't exist with an empty string.
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 02:40:36 PM
Hey! Thanks, but still the same output with


  echo "0000068: FF 3F" | xxd -r - $f
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: names_are_hard on October 29, 2021, 03:13:55 PM
Sorry, don't have a Mac, so I have no way to test.  I don't see any other mistakes, but I try and avoid using shell scripts, because they are hard to debug.  So I'm not very good at them.
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: Walter Schulz on October 29, 2021, 03:37:23 PM
Is shell in macOS case-insensitive for variables? Because $FILE and $file?
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 05:19:35 PM
Thanks, but case doesn't seem to make a difference. I have this now:

#!/bin/bash
FILES="/Users/me/Desktop/media/*.MLV"
for f in $FILES
do
  echo "0000068: FF 3F" | xxd -r - $f"
  cat "$f"
done

Random gibberish output has been replaced with this:

line 6: unexpected EOF while looking for matching `"'
line 8: syntax error: unexpected end of file

A Google search reveals stuff about making sure text editor is using correct double quotes – which Sublime text is.
Anyone spot any schoolboy errors?  ::)


Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: Danne on October 29, 2021, 05:27:09 PM
echo "0000068: FF 3F" | xxd -r - $f"
Should be.
echo "0000068: FF 3F" | xxd -r - "$f"

Still, not sure your wildcard line will work but try around some and you´ll probably find the right syntax. Is xxd applying the change on a single file, tested that?
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 05:37:04 PM
Thanks. Looked promising – as the code syntax highlighting in Sublime Text looked more correct – but now random characters on output has returned!

Original command on a single file always worked. I used this:


echo "0000068: FF 3F" | xxd -r - Input.MLV
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 05:46:04 PM
@Danne

You could be right about the wildcard. If I remove it,


FILES="/Users/me/Desktop/media/"

the output is without the crazy characters, now just

xxd: /Users/me/Desktop/media/: Is a directory
cat: /Users/me/Desktop/media/: Is a directory


Sheesh!
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: Danne on October 29, 2021, 05:51:30 PM
What is cat doing here?

If you´re in a folder with mlv files and do this?
for f in *.MLV
do
  echo "0000068: FF 3F" | xxd -r - "$f"
done
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 06:17:56 PM
get

line 2: syntax error near unexpected token `|'
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: timbytheriver on October 29, 2021, 06:37:28 PM
Ok. Thanks all for help. Now it works fine:


#!/bin/bash
FILES="/Users/me/Desktop/media/*.MLV"
for f in $FILES
do
echo "0000068: FF 3F" | xxd -r - "$f"
done



Save as foo.sh script, run chmod u+x on it, and run it from anywhere (not within the MLV folder itself).  :)
Title: Re: Batch modify MLVs using bash on Mac – Help please!
Post by: Danne on October 29, 2021, 08:12:15 PM
Also possible to place a script next mlv files in a folder and double tap it containing following:
#!/bin/bash
cd "$(dirname "$0")"
for f in *.MLV
do
  echo "0000068: FF 3F" | xxd -r - "$f"
done


Name the script foo.command and run chmod u+x on the script before double tapping.