user2167582 user2167582 - 1 month ago 5
Bash Question

how to call long functions iteratively in bash script

I want to iterate through a list of videos and convert them from .avi to .mp4 using a script, which takes about ~1 min for each file.

find . -name "*.avi" -type f | while read FILE; do
HandbrakeCLI [flags] $FILE # execute 30 sec ~ 2 min function here

problem is, this seems to be only running on the first file, as though successive invocations are blocks.

How can I spawn asynchronous threads to handle each case?

This is a duplicate of that other issue which is trying to solve new processes of ssh, and the solution is based on arguments available to ssh.
I am asking for a way to spawn new bash threads in an iterator, maybe theres a duplicate ticket that can direct me that way?


Assuming your issue is the HandbreakCLI script is only called once, the root cause might be it behaves differently depending on whether the standard input is a terminal or not, or maybe does it drains its stdin. In such case, here is a way to overcome the issue from within find:

find . -type f -name  "*.avi" -exec sh -c 'FILE="$1" HandbrakeCLI &' sh {} \;

To trace what this command is running, you can modify it that way:

find . -type f -name  "*.avi" -exec sh -c 'set -x; FILE="$1" HandbrakeCLI &' sh {} \;