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.
1 Answer 1
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
-
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– nurdglaw2020年07月03日 13:24:07 +00:00Commented 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.2020年07月03日 13:50:19 +00:00Commented Jul 3, 2020 at 13:50
I've been writing the scripts by using cat
– Nice. Only two steps to butterflies. :D Just in case: how exactly do you usecat
for this task?