LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-13-2011, 03:07 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Question 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
a.c  a.out  b.c  cscope.in.out  cscope.out  cscope.po.out  Makefile  tags
[river@localhost a]$ ./a.out  a.c
./a.out, a.c
[river@localhost a]$ ls | xargs ./a.out
./a.out, a.c
[river@localhost a]$
What's wrong, only the first file listed can be dealed.

The source code of a.out is :
Code:
#include <stdio.h>

int main( int argc , char* argv[] ) {
    printf("%s, %s\n", argv[0], argv [1]);
    return 0;
}
Can anyone help me ?
Thanks!
 
Old 07-13-2011, 03:11 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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].
 
Old 07-13-2011, 03:27 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
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`
, how can xargs be useful?
 
Old 07-13-2011, 04:17 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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
 
Old 07-13-2011, 07:01 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 *
???
 
Old 07-15-2011, 03:56 AM   #6
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks for everyone!
I got it.
 
Old 07-15-2011, 04:01 AM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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
which is what you originally thought xargs would have done, if I understood you correctly (but still, do modify your program to handle multiple arguments, if it is suitable).

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.

Last edited by b0uncer; 07-15-2011 at 04:13 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to run a program remotely using a custom environment? jerunh Programming 5 06-14-2011 09:57 AM
How to run custom command when USB drive automounted - Gnome musther Linux - Software 1 05-15-2008 10:20 AM
Custom Command? - Excuting Program SilverX Slackware 11 09-20-2004 03:49 PM
command to run a program stupidloser Linux - Newbie 1 08-11-2004 02:55 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration