2

I need to recursively find files of one type in directory. And then for each file found execute script with found file as parameter:

#!/bin/bash
find /mnt/storage/content/www/html/smartsite/src/data/resource/ ! -path "*/tmp/*" -regextype posix-awk -iregex "(.*.toConv)" -exec /mnt/storage/content/www/html/smartsite/cron/VideoConvert/convert {} \;

It works as should, however execution stops when called script (convert) throws error. I want to prevent this and continue execution for next files found. No matter what I do, for example

/convert || true {} \;

or

/convert {} || true \;

It always throws an error "No paremeters for exec". What am I doing wrong?

2
  • 1
    Out of curiosity, could you explain throws error or provide the script? Execution of find shouldn't stop on stderr or even if the script returns an error exit code (-exec will just become false, which will prevent expressions after the -exec from executing, and cause find to move on to the next file. But since there are no expressions after the -exec in your command, this shouldn't matter) Commented Oct 4, 2014 at 8:58
  • There is ffmpeg call inside "convert" script. If ffmpeg fails, find stops searching. I wanted to prevent this and go to the next file found. So I either I neeed to make sure that "convert" script won't throw any error to stderr or add || true to find/exec to pass "true" exit code to find, even if script fails Commented Oct 4, 2014 at 10:26

3 Answers 3

2

Try this

find ... -exec bash -c "convert {} || true" ";"

or

find ... -print0 | xargs -r -0 -i convert {}
Sign up to request clarification or add additional context in comments.

Comments

2

The immediate problem is that the parsing order doesn't work the way you think it does. The command:

find /.../resource/ ! -path "*/tmp/*" -regextype posix-awk -iregex "(.*.toConv)" -exec /.../convert || true {} \;

Gets parsed by the shell into two commands (separated by ||):

find /.../resource/ ! -path "*/tmp/*" -regextype posix-awk -iregex "(.*.toConv)" -exec /.../convert
true {} \;

...and since the \; is part of a separate command, find never sees it, and complains that you didn't give it a complete -exec sequence. You could quote/escape the || so it gets passed to find as part of the -exec sequence, but that wouldn't do what you want either, because -exec just runs a single command, and passes what it's given as arguments to that command, so the convert script would get || and true as arguments...

In order to do what you're trying to do, you'd actually have to have -exec run another shell, and have that subshell run the convert script and true. This can be done, although it's a little messy, but it won't solve the actual problem either, because the actual problem isn't what you think it is.

As @BroSlow said, find ... -exec does not stop just because the command returned an error status (or output something to stderr). Something else is making find stop, so overriding the exit status is not going to fix the problem. You need to figure out the actual problemis, and attack that.

Comments

1

This seems to be ok:

convert {} \; || true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.