Tom Lord
Tom Lord

Reputation: 28305

Bash script to find and execute more complex command

Ok, so I have a bunch of test_xx and validate_xx files that I want to execute in the following way:

./path/test_01 | ./server | ./path/validate_01

./path/test_02 | ./server | ./path/validate_02

... And so on.

Now then, I want to create a run_all script that will locate all of these other scripts and run them all in this way.

I can use the following code to find and execute only, for example, test_01:

find ./*/ -name test_01 -exec {} \;

So, I have two problems:

  1. (Important!) How can I make bash execute the more complicated line above, with piping and two unknown directories to search for? I can only find how to execute a single command.

  2. (Less important, but still an issue...) What would be the best way to loop this script, so that it executes all test/validate scripts in the directory, then stops? The scripts are currently named test_01, test_02, ..., test_26 (and similarly for validate_xx) - but I want to script to still work, without changing, if I add test_27 etc.

Upvotes: 0

Views: 867

Answers (2)

Amadan
Amadan

Reputation: 198314

If they don't span multiple directories, you don't need to use find:

for test in path/test*; do $test | ./server | ${test/test/validate}; done

BTW, useful tidbit: ${a/b/c} syntax says: take value of variable a, and replace b with c.

If you do need find, then you can wrap redirection inside a shell script:

find dir -exec sh -c '... | ... | ...' \;

EDIT: in a bit more detail,

find . -name test\* -exec sh -c '
  test={}
  validate=${test/test/validate}
  $test | ./server | $validate
' \;

Upvotes: 1

choroba
choroba

Reputation: 241858

Use a for loop. For example, you can loop over the tests and extract the number from them via Parameter expansion:

for test in ./path/test_[0-9][0-9] ; do
    "$test" | ./server | ./path/validate_${test: -2}
done

Upvotes: 0

Related Questions