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.

nurdglaw
  • 135
  • 5
  • Can you edit your question and include the script? – Andy Dalton Jul 02 '20 at 16:19
  • What do you mean by "_source it into my bash shell_"? – roaima Jul 02 '20 at 16:44
  • `I've been writing the scripts by using cat` – Nice. Only two steps to [butterflies](https://xkcd.com/378/). :D Just in case: how exactly do you use `cat` for this task? – Kamil Maciorowski Jul 02 '20 at 17:03
  • @Andy Dalton I have edited my question to address, I think, all your questions. Of course the **** think is now working OK, which is very strange because I made sure I was doing EXACTLY what I said in the question and was getting exactly the errors I was quoting. – nurdglaw Jul 02 '20 at 17:09
  • Read [`find` and globbing (and wildcards)](https://unix.stackexchange.com/q/55334/108618). Use an editor; not `echo` nor `cat` nor butterflies. – Kamil Maciorowski Jul 02 '20 at 17:14
  • 2
    The quotes seem to have disappeared. That will cause a problem. And echo will need some extra quoting. – ctrl-alt-delor Jul 02 '20 at 17:14
  • 1
    Why are you sourcing something to run a script? – Andy Dalton Jul 02 '20 at 21:29
  • @Andy Dalton I'm sourcing it because I'm too idle to change the permissions to allow me to execute it directly. Does it make a difference? – nurdglaw Jul 02 '20 at 21:41

1 Answers1

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
terdon
  • 234,489
  • 66
  • 447
  • 667
  • 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 Jul 03 '20 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 Jul 03 '20 at 13:50