Вопрос

I am trying this:

ls | sed 's/.*/"&"/' | xargs sh -- script.sh

for files:

  • -test 23.txt
  • test24.txt
  • te st.txt

but after this, script.sh executed only for:

-test 23.txt
Это было полезно?

Решение 2

xargs, by default, assumes that the command it is expanding can take multiple arguments. In your example, xargs would have executed

sh -- script.sh "-test 23.txt" "test24.txt" "te st.txt"

If your script only echoes its first argument, then you'll only see -test 23.txt

You can tell xargs to execute the command for every input by using the -n1 flag.

In many cases, xargs is not what you want, even coupled with the find command (which has a useful -exec action). When it is what you want, you usually want to use the -0 flag coupled with some flag on the other side of the pipe which delimits arguments with NUL characters instead of spaces or newlines.

Другие советы

Better use a glob :

./script.sh *

No need to add double quotes like you try

If your script don't loop over the arguments, try this :

for i in *; do ./script.sh "$i"; done
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top