How to use command xargs to run my custom program?
I want to list all files by command ls , then use command xargs to deal the results.
The result is the following: Code:
[river@localhost a]$ ls The source code of a.out is : Code:
#include <stdio.h> Thanks! |
The problem is not with xargs, but with your program. xrags gives the output of ls to your program, but the program prints out only the first two arguments argv[0] and argv[1].
|
Thanks!
I have thought the command xargs can spilt the result and then deal with every piece . So I can just use command Code:
./a.out `ls` |
You can look at the xargs man page to find how to set the max number of arguments to dump on every iteration of the command. However what you should do is to modify your program to iterate through all the given arguments, in my opinion.
I also don't like using ls like that, it will give you trouble some day. http://mywiki.wooledge.org/ParsingLs |
Do not parse the output of ls. It's meant for human reading, and parsing it could cause huge trouble, especially if filenames contain whitespace.
What's wrong with this: Code:
./a.out * |
Thanks for everyone!
I got it. |
If you insist on working on the files one by one, why not just build a list of files using your friendly neighbourhood shell (which I assume to be Bash here; for other shells, see their documentation) and then loop on that list:
Code:
for i in *; do ./a.out "$i"; done Edit: ha, now that I read the linked page in post #4, I noticed that the for loop was mentioned there as an option to ls :) Great. |
All times are GMT -5. The time now is 05:11 PM. |