0

I learned from answers under this question: Making xargs work in Cygwin that option xargs -I does not work properly under Cygwin. There were some workarounds, but unfortunately it does not help in my case.

My question is, how can I approach the same result as:

..somthing that produces multiple lines.. | xargs -I % command -option1 % -option2 %

under Cygwin environment?

Edit:

To clarify, I would like to get some values from stdin and invoke the "command", putting them into two places as its arguments "%". I would like to invoke my command multiple times on a data produced by the "something".

Example 1: (i haven't been programming in cpp for a huge time so please forgive me mistakes)

find -name *.cpp | cut -d. -f1 | xargs -I % gcc -o %.o -I %.h %.cpp

Example 2:

cat songs_to_process.txt | xargs -I % convert --format=mp3 --source=%.avi --output=%.mp3
mpasko256
  • 811
  • 14
  • 30

1 Answers1

2

You don't need xargs for this job at all -- not in any of your examples.

find . -name '*.cpp' -print0 | while IFS= read -r -d '' filename; do
  basename=${filename%.*}
  gcc -o "${basename}.o" -I "${basename}.h" "${basename}.cpp"
done

...or:

while IFS= read -r song; do
    song=${song%$'\r'} # repair if your input file is in DOS (CRLF) format
    convert --format=mp3 --source="$song".avi --output="$song".mp3
done <songs_to_process.txt

See BashFAQ #1 for an introduction to best practices for processing inputs line-by-line in bash.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441