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_imagescript 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_fileis 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 -L1to 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_imagerather thanimages_file, but if you're set on xargs, okay. I don't see anything in the xargs man page that suggests-L1would help you, but I do see-s somelargenumbermight 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_imageloop 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_imagescript to read these files from a location. I want to know whetherxargs -L1anyway uses the same mechanism aswhile. My boss insists that I do not usefororwhileloops. So ifxargsalso uses loops, why not use usewhileloop? Either that or I want to be able to usexargsto 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:37xargsover 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_imageprogram 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_imageorprocess_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_imageaccepting 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_imageprogram 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