2

I'm a long-time find and bash user, but this seems to be the first time I've tried to run a find command in a script.

I'm trying to loop through all the project source files to find the first argument passed to a few functions. I've written and tested a little awk script that seems to do the job and I can run it from the command line via

find sources -name '*.cpp' -exec awk -f foo.awk {} \;

The trouble comes when I try to put it in a script, via

echo find sources -name *.cpp -exec awk -f foo.awk {} \\\; >foo.sh

so that foo.sh contains

find sources -name *.cpp -exec awk -f foo.awk {} \;

If I now source it into my bash shell via

source foo.sh

I get the error

find: missing argument to `-exec'

I thought that was a quoting issue, so I doubled the backslash before the final semi-colon. It made no difference. Just for laughs, I tried trebling the backslash. Still no difference.

Full disclosure: I'm running on Windows 10, under cygwin, using GNU Awk 5.1.0. I've been writing the scripts by using cat, so I should be clear of any problems with line endings

Update

Except that I don't. Running with a different awk script, the script that I've just pasted into this question works fine. I shall try again tomorrow and post my findings.

Thanks for your forbearance.

8
  • Can you edit your question and include the script? Commented Jul 2, 2020 at 16:19
  • What do you mean by "source it into my bash shell"? Commented Jul 2, 2020 at 16:44
  • I've been writing the scripts by using cat – Nice. Only two steps to butterflies. :D Just in case: how exactly do you use cat for this task? Commented Jul 2, 2020 at 17:03
  • 2
    The quotes seem to have disappeared. That will cause a problem. And echo will need some extra quoting. Commented Jul 2, 2020 at 17:14
  • 1
    Why are you sourcing something to run a script? Commented Jul 2, 2020 at 21:29

1 Answer 1

3

This is almost certainly because you're not quoting the *.cpp so it is being expanded before being passed to find or, if you have *cpp files in your current directory, before even writing to the script.

If I'm right, this should do what you need:

echo "find sources -name '*.cpp' -exec awk -f foo.awk {} \;" >foo.sh
2
  • I'm accepting this answer since it almost certainly resolves the problem I was having yesterday. However, since I still cannot reproduce the original problem, I cannot say for certain. (At present, even when foo.sh contains `` (without quotes) it works as desired).
    – nurdglaw
    Commented Jul 3, 2020 at 13:24
  • 1
    @nurdglaw this sort of thing is really annoying. The details will always depend on exactly what files/directories were present when you ran the command. Since the problem is caused by the shell expanding *.cpp to the matching file names, the details will change depending on exactly what the expansion returns. So to reproduce it, you need to have exactly the same expansion result as when you originally ran it.
    – terdon
    Commented Jul 3, 2020 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .