0

Is it possible to specify the number of arguments that xargs takes without doing it explicitly with -n option, but implicitly with a delimiter, so that the single parameters would be delimited with a space, but a group of arguments that xargs takes at once would be delimited with for example '\n'?

I want to be able to pass a flexible number of parameters to a script: Those parameters are stored in list like this one:

param1 param2
param1 param2 param3
param1

It should be possible to pass one line of parameters at a time to the script. I do not want to use any proxy scripts and I do not want to write a temporary list of files or command to the disc. I want to read the parameters from the file, alter them "on the fly" (e.g. remove every other line), and redirect them with xargs to the destination script.

What I tried is to use '\n' as an argument delimiter, but than all params in a line are interpreted as one argument, even though they are delimited with a space.

2
  • Hardcoding the program invocations, or calling a script that expects 6 arguments and splits them up into these three calls isn't an option?
    – Daniel Beck
    Jan 14, 2012 at 11:52
  • No, this is just an example, the list can be of any length. Also I do not want to do this with a proxy script that splits the space delimited arguments.
    – crenate
    Jan 14, 2012 at 11:59

2 Answers 2

1

Q

Want I want to achieve is to pass the following list through xargs but one line at a time:

A

$ cat t7.txt
param1 param2
param1 param2 param3
param1

$ cat t7.sh
#!/bin/bash
cat t7.txt | while read line
do
  echo $line | xargs echo XXX
done

$ ./t7.sh
XXX param1 param2
XXX param1 param2 param3
XXX param1

But there are easier ways to achieve this output without using xargs so you may want to clarify your question.


Update

$ while read line; do echo "< $line >"; done
p1 p2
< p1 p2 >
p1
< p1 >
p1 p2 p3
< p1 p2 p3 >

I don't think xargs is the answer to your task. I suspect you would be better off using some script in place of xargs.

3
  • Thanks, but I do not want to write intermediate files. I edited the question to clarify. I would like to do this "on the fly".
    – crenate
    Jan 14, 2012 at 14:46
  • @mithy: Answer updated accordingly. Jan 14, 2012 at 15:22
  • Yes, you are right, the loop does what I want. Thank you!
    – crenate
    Jan 17, 2012 at 11:28
1

With GNU Parallel you can do:

cat yourfile | parallel -q bash -c 'echo {}'

If it is acceptable that the parameters are joined as one, then you can even do:

cat yourfile | parallel echo {}

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .