3

I have a source tree which, when make is run, produces several executables named "001", "002", and etc. I'm trying to write a script which will find all of these executables in my source tree, and then execute them. I have this so far:

find build/ -type f -executable | ack --nocolor "\d{3}$"

Which lists the executables that I want to execute correctly.

My question is, how do I then run all of them? I thought perhaps some combination of xargs and exec would do it, but exec seems to try replacing the current shell with the command, which isn't what I want.

2

Try:

$ find build/ -type f -executable | ack --nocolor "\d{3}$" |
while read prog
do
    "$prog"
done
  • I think @1_CR's answer is cleaner to just execute the files, but I'm accepting this because it turns out that it's useful to be able to do other stuff inside the while besides just executing the file, and it's trivial to extend this version. Thanks! :) – CmdrMoozy Aug 14 '14 at 18:01
  • 1
    @CmdrMoozy, you don't need to step outside find in order to loop. For example find build/ -type f -name '*[0-9][0-9][0-9]' -executable -exec bash -c 'for f; do cksum "$f"; done' _ {} + – iruvar Aug 14 '14 at 18:12
4

This may work

find build/ -type f  -executable -exec sh -c 'exec "$1"' _ {} \;

Or to filter to just the 001, 002 .. files

find build/ -type f -name '*[0-9][0-9][0-9]' -executable  -exec sh -c 'exec "$1"' _ {} \;
  • The OP want to filter the result of find before execute it. – cuonglm Aug 14 '14 at 17:47
  • @Gnouc, I am not sure if he's using ack for listing or for filtering. Anyways, i'll add a filter – iruvar Aug 14 '14 at 17:51
  • ack is basically a nicer version of grep - so it's mostly for filtering. This build system produces some other files that are marked executable besides the actual compilation results. – CmdrMoozy Aug 14 '14 at 17:52
  • @CmdrMoozy and Gnouc, added filtering – iruvar Aug 14 '14 at 17:55
  • +1 because you solved the problem using only find. Thanks! :) – CmdrMoozy Aug 14 '14 at 18:00
1

One of the things find is good at is selecting files that match criteria, so you can use it to pick out the files whose names consist of three digits.  And then you can use this simpler form:

find build -name "[0-9][0-9][0-9]" -type f -executable -exec {} ";"

And you might be able to use -regex '.*/\d{3} if you can find the right -regextype to go with it.

1
find -type f -name '00[123]' -exec env - {} \;

Add in whatever environment you want to declare for the different executables. Maybe 2>/dev/null if there are non-executable 00[123] files lying around and you don't want to hear about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.