Wednesday, April 29, 2009

how to pipe data to a program that only allows command line input?

This come sup all the time because allot of programs don't allow you to simply pipe data to them. Remember when we pipe data we use the "|" symbol to chain commands like this: sort | uniq. When we have a program that doesn't allow this we can use a program called xargs. xargs allows you to take the output from one program and pass it to another program as a command line argument. I have used xargs in examples of other blog posts. Here is how it works:

let's say I want to pass the names of all the files in a directory to a progrma that only takes input from the command line. I would do the following:

> ls | more | xargs -n 1 someprogram

This will take the file names and pass them to the program someprogram on at a time.

Commands used in this post: ls, more, xargs

No comments: