Showing posts with label xargs. Show all posts
Showing posts with label xargs. Show all posts

Wednesday, October 6, 2010

Command Line Too Long

I spent the weekend writing scripts to handle various situations where a very long command line was necessary. Usually it was handling one command that had to be repeated for each file in a directory.

For example, pkg_add on FreeBSD doesn't handle more than 200 packages at a time on the command line. So I spent the first part of the evening writing little scripts like:

# sh
 for pkg in *
 do
 pkg_add $pkg
 done

Those scripts work, but later I found a utility designed just for this particular difficulty: xargs. 

According to the man page:

The xargs utility reads space, tab, newline and end-of-file delimited arguments
from the standard input and executes the specified utility with them as
arguments.

Basically it means you pipe a list of "arguments" to xargs. Xargs sucks up the list and then feeds them back one (or more) at a time to the specified program until they are all gone. For my application, this worked like a charm. My already small script was reduced to:

ls | xargs  -n1 pkg_add