복합 명령을 파이프할 수 있나요?

복합 명령을 파이프할 수 있나요?

내 질문은: 파이프는 어떻게 할 수 있나요/어떻게 할 수 있나요?복합 명령( { list; })? 부록 B를 참조하세요. 부록 A는 비교 목적으로 제공됩니다.

부록 I:

$ cat << EOF | xargs -n 1 echo           
foo
bar
qux
EOF
foo
bar
qux

첨부 B:

$ cat << EOF | xargs -n 1 sh -c '{ var="$1"; echo "$var"; }'
foo
bar
qux
EOF


man sh:

-c               Read commands from the command_string operand
                            instead of from the standard input.  Special parame‐
                            ter 0 will be set from the command_name operand and
                            the positional parameters ($1, $2, etc.)  set from
                            the remaining argument operands.

답변1

문제는 sh -c "something"또 다른 매개변수가 필요하다는 것이다 $0.

두 번째 예에서는 하나를 제공하지 않으므로 $1빈 문자열이고 3개의 빈 줄이 표시됩니다. 사용

cat << EOF | xargs -n 1 sh -c '{ var="$1"; echo "$var"; }' inlineshellscript
foo
bar
qux
EOF
food
bar
qux

일반적 으로 대신 을 사용 $0합니다 .inlineshellscriptshinlineshellscript

관련 정보