1

I would like to run this command on all .wa files within this folder (ideally with subfolders): ./runtest Basic prog -t abc.wa

The script I wrote returns an error:

for filename in ./examples/*.wa;
do
    ./runtest Basic prog -t "${filename}"
done

When running this with ./testscript I get: Syntax error: word unexpected (expecting "do")

Can the script also add to the console output the name of the current file?

1
  • Does your script has DOS line endings? Commented Sep 9, 2015 at 16:55

3 Answers 3

1

You don't need the semicolon unless the do is on the same line.

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

1 Comment

That is not a problem in bash. In fact, no in any shell. The semicolon is not required, but is allowed. And perfectly correct.
0

This command should make it:

find ./examples -type f -name '*.wa' -exec ./runtest Basic prog -t "{}" \; -print

find is your friend. Have a look at its man page, it is worth reading.

7 Comments

This works but isn't materially different here and may be much more costly depending on what else is in the examples directory.
What do you mean by more costly? As the initial for-do-done loop spawns a new shell for each file and as Pete asked for ideally with subfolders I guess the find way should be rather efficient.
The original doesn't spawn sub-shells at all. It does run runtest but you can't avoid that. I'd missed ideally with subfolders which makes find actually useful here. My apologies for that. And the costly was if subfolders weren't desired for the search and they did exist (and were large) then find would search them and the glob wouldn't.
Sorry to insist, it does spawn a shell because it runs runtest. So, on a pure efficiency point of view, using find is at least equivalent. And the good point is that it descends in the sub-directories.
Of course it runs runtest. So does yours. That's a requirement. But it doesn't spawn a sub-shell to do that. It just executes it directly. Why do you think there's a sub-shell involved? At what point?
|
0

To run in all subfolders, use ** as this:

#!/bin/bash

( shopt -s globstar
for filename in ./examples/**.wa
do
    echo "${filename}"
done
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.