kjh
kjh

Reputation: 3411

Unix: Xargs won't terminate

I've read through the man pages for xargs a dozen times and I still can't figure out how to get it to terminate. When I call this at the shell: xargs echo this is xargs I only see output when I press ctrl+d. Isn't there a way to specify that I want xargs to stop after reading a certain number of arguments? So that when I type xargs echo this is xargs and press enter I immediately see this is xargs without having to press ctrl+d?

I have tried setting the eof flag with -e and -E and neither worked for me when doing so like this:

xargs -Ez echo this is xargs z

I'm asking because I'm trying to write a C program that forks a child and uses execl to call xargs, but my program hangs, so before I try to continue coding the program I want to at least be able to call xargs just right from the command line.

Upvotes: 0

Views: 2618

Answers (2)

Fidel
Fidel

Reputation: 1037

Xargs executes utility/command once it got the input from standard input file.

When you are trying executing xargs echo something something its waiting for the input from the standard input file, once you pressed Ctr+d which means EOF, Xargs execute the command with the arguments from the standard input file.

Below are the examples.

bash-$ echo "Testing xargs command" | xargs echo 
# echo executes by reading the input  
#file from pipe ( which redirects first commands output into second commands input )
Testing xargs command #output
bash-$ 
bash-$ 
bash-$ xargs echo #waits until CTRL+D is pressed
Testing xargs command
Testing xargs command  #output
bash-$ 
bash-$ 
bash-$ xargs -E EOF echo # xargs option -E specifies End-OF-File-String, here EOF
Testing xargs command
EOF
Testing xargs command #output  

Hope this helps

Upvotes: 1

Beano
Beano

Reputation: 7851

xargs reads input from stdin which it then appends to the parameters you have passed to it. What you are seeing is that when you run xargs it is awaiting input from stdin (apparently hangs), when you type ctl+d - this is EOF to stdin therefore xargs processes all it has read.

An example of xargs to grep for the string "string" in all files named *.conf

find . -name \*.conf -print | xargs grep string

Upvotes: 0

Related Questions