Sample Header Ad - 728x90

In the context of {ARG_MAX}, how robust is `find . -exec sh -c 'exec tool "$@" extra-arg' find-sh {} +`?

2 votes
0 answers
47 views
Suppose I want to do this: find . -exec tool {} extra-arg + It doesn't work and I know why: -exec … {} + does not allow extra-arg(s) between {} and +. So be it. It seems I can inject the extra-arg by using a shell, like this: find . -exec sh -c 'exec tool "$@" extra-arg' find-sh {} + My question is: **how robust is this method?** I mean in the context of {ARG_MAX} and possible "argument list too long" error. I know find … -exec … {} + is supposed to group pathnames in sets to avoid the error. At first glance the command from the expansion of tool "$@" extra-arg should be shorter than what find executes, so if find manages to avoid "argument list too long" then exec tool … will avoid it as well; but: 1. I'm not sure if the relevant structure will contain tool or /full/absolute/path/to/tool which may be long and thus possibly make the structure exceed {ARG_MAX}, despite the effort to not exceed {ARG_MAX} taken by find earlier. 0. I'm not sure if sh may add something to the relevant structure and thus possibly make the structure exceed {ARG_MAX}, despite the effort to not exceed {ARG_MAX} taken by find earlier. **Assuming that find does a good job avoiding "argument list too long", can I assume my exec tool … invoked in the inner shell will avoid the error as well?** Clarification: - Yes, I'm asking about my attempted solution, like in the XY problem; but I'm *deliberately* asking about my attempted solution because I want to understand possible flaws of it. I know I can use -exec tool {} extra-arg \; and avoid the problem. I know I can pipe (preferably null-terminated strings) to xargs and let xargs deal with {ARG_MAX}. If my attempted solution is not robust and if some improvement can make it robust then I'm interested, but only if -exec … {} + is kept. - I know that some implementations of find do not try to get as close to {ARG_MAX} as possible , but according to the POSIX specification find does not have to leave any room we could take advantage of later. I do not have any particular implementation of find in mind. In this matter I will appreciate generic answers or answers that compare many implementations. - In the context of (1), the question is specifically about Linux. If a good answer mentions or compares to other Unices then fine, I don't mind extending the scope this way; but I do not require this. - In the context of (2), I do not have any particular implementation of sh in mind.
Asked by Kamil Maciorowski (24294 rep)
Nov 28, 2024, 01:45 PM