Say I have an executable script process_image
that performs actions on a base 64 encoded image. I am storing every image in a file images_file
line by line. Every line of images_file
is a base 64 encoded image. Some of the lines are very long therefore the following returns xargs: argument line too long
:
cat images_file | xargs -L1 process_image
I wanted to modify process_image
to take the entire stdout from cat images_file
and then loop over each line using a simple while loop, but my colleagues have advised against this approach. Does xargs -L1
also internally use the same mechanism as while
? How would using xargs
be more desirable than using a while
? What is the maximum argument length that xargs
can handle and is there any way to overcome this while maintaining the cat <file> | xargs -L1 <executable_script>
approach?
process_image
script or program read the image data from stdin, or can you give it a command-line argument that specifies a file to read the image from (for example-f /path/to/image-file
? – Sotto Voce Jul 14 '22 at 07:27images_file
is being generated in runtime by a previous process. I could find many workarounds to this, but I am not being allowed to. I want to know if I can specifically usexargs -L1
to allow an argument whose length exceeds the default allowed limit. – sriganesh Jul 14 '22 at 07:32xargs
? Try dumping the value of ARG_MAX, usinggetconf ARG_MAX
– Inian Jul 14 '22 at 07:37process_image
rather thanimages_file
, but if you're set on xargs, okay. I don't see anything in the xargs man page that suggests-L1
would help you, but I do see-s somelargenumber
might help. (although the man page says the default value is ARG_MAX - 4096, so there doesn't seem to be a lot of space to gain) – Sotto Voce Jul 14 '22 at 07:41process_image
loop over input? – muru Jul 14 '22 at 07:53-L
? If not, a simple loop reading one line at a time might suffice – Chris Davies Jul 14 '22 at 08:25process_image
script to read these files from a location. I want to know whetherxargs -L1
anyway uses the same mechanism aswhile
. My boss insists that I do not usefor
orwhile
loops. So ifxargs
also uses loops, why not use usewhile
loop? Either that or I want to be able to usexargs
to allow a size larger thanARG_MAX
. I am a fresher with no prior experience in shell scripting and I am working under many heavy design constraints (no loops, no intermediate files). – sriganesh Jul 14 '22 at 08:37xargs
over that? Check to see if it runs, and what the exact length of the file was. – ilkkachu Jul 14 '22 at 08:46&
truly runs processes in parallel… – Stephen Kitt Jul 14 '22 at 10:05process_image
program take its image as a command-line argument? That would be reasonable for very small amounts of data, but is absurd when you're talking about many kilobytes or more of data, and likely to run into ARG_MAX command-line length limits (especially on systems that have smaller ARG_MAX than Linux does). Instead, your program should take its data from a file or files, with the argument being the filename(s) containing the image(s), or from stdin. That is what you need to fix. – cas Jul 14 '22 at 11:27echo -n "$imagedata" | process_image
orprocess_image <<<"$imagedata"
or perhaps evenprocess_data -f <( echo -n "$imagedata" )
to give the data to the command without creating oversize command-line arguments (which my first and third examples still do). But they depend onprocess_image
accepting file data on stdin or using an argument for passing a file path on the command line, which is still not known in this discussion. – Sotto Voce Jul 14 '22 at 13:56process_image
program so that it reads its data from stdin (or from a file) - taking bulk data from command line args is beyond absurd, it is insane. – cas Jul 14 '22 at 16:16