2

my script is not giving me any output

for file in ./*
do
    if [ "$file" = *.mov ];
    then
        ffmpeg -i $file -an -f framemd5
    fi
done

whenever I run it it just give me back my prompt immediately.

2 Answers 2

2

Because your test:

if [ "$file" = *.mov ];

is false.

If, for exampe, the current directory has these files:

file1.mov
file2
file3.txt
file4.mov

Then the $file variable will be set as follows through each iteration:

  1. ./file1.mov
  2. ./file2
  3. ./file3.txt
  4. ./file4.mov

But the right-hand side of the test will remain to be "file1.mov file4.mov", so each test is:

  1. if [ "./file1.mov" = file1.mov file4.mov];
  2. if [ "./file2" = file1.mov file4.mov];
  3. if [ "./file3.txt" = file1.mov file4.mov];
  4. if [ "./file4.mov" = file1.mov file4.mov];

...neither of which is ever true.

If you want to loop through all the .mov files in the current directory, use this instead:

for file in ./*.mov; do
    ffmpeg -i $file -an -f framemd5
done

By the way, you should always be ready for files with spaces and other annoying characters in the name, so this would be a bit more robust:

for file in ./*.mov; do
    ffmpeg -i "$file" -an -f framemd5
done

As it will put quotes around the file name.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, dovetalk. A very helpful answer.
If I want the script to read not from the current directory I am in but from any directory I place in the CLI, how do I do that?
I'm not sure what you mean by "in the CLI", but if you want it to be the first parameter to the script, you just change ./*.mov to "$1"/*.mov
Now what if I the directory chosen as the first parameter has mutiple directories within it, each with with multiple .mov files. How do I write the script to loop through all .mov files within that directory at various depths?
2

Assuming you are using bash, you need to use the [[ command for pattern matching.

for file in ./*
do
    if [[ "$file" = *.mov ]];
    then
        ffmpeg -i "$file" -an -f framemd5
    fi
done

However, it's simpler to just match the .mov files in the first pattern as shown by dovetalk.

1 Comment

Thank you, chepner, as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.