bash: 파일 이름이 포함된 여러 공백 전달

bash: 파일 이름이 포함된 여러 공백 전달

Bash 스크립트에서는 PDF를 페이지 단위로 래스터화하여 단일 파일로 만들고 결과 단일 PNG를 다음과 같이 다시 PDF로 병합합니다.

convert -monitor /path/to/1.png /path/to/2.png /path/to/3.png ... output.pdf

스크립트에서 여전히 가지고 있는 유일한 문제는 공백이 있는 파일을 올바르게 처리하지 못한다는 것입니다. 제가 시도한 몇 가지 사항은 다음과 같습니다.

newfile=$(sed -r -e 's| |\\ |g' <<< "$tmppath$curr.png")
echo "DEBUG: newfiles : $newfiles"
filearray[$curr-1]="$newfiles"
echo "DEBUG: filearray: ${filearray[*]}"

그러면 (페이지/파일당) 다음이 생성됩니다.

DEBUG: newfiles : /tmp/pngpdf/file\ with\ spaces/1.png
DEBUG: filearray: /tmp/pngpdf/file\ with\ spaces/1.png

나중에 두 개의 디버그 메시지가 나타납니다.

echo "DEBUG: filearray: ${filearray[*]}"
echo "DEBUG:            ${filearray[0]}, ${filearray[1]}, ${filearray[2]}, ..."

filearray여러 파일/페이지가 어떻게 보이는지 확인하세요 .

DEBUG: filearray: /tmp/pngpdf/file\ with\ spaces/1.png /tmp/pngpdf/file\ with\ spaces/2.png /tmp/pngpdf/file\ with\ spaces/3.png /tmp/pngpdf/file\ with\ spaces/4.png /tmp/pngpdf/file\ with\ spaces/5.png
DEBUG:            /tmp/pngpdf/file\ with\ spaces/1.png, /tmp/pngpdf/file\ with\ spaces/2.png, /tmp/pngpdf/file\ with\ spaces/3.png, ...

나는 다음을 명확하게 볼 수 있습니다.

  1. 각 파일에는 배열 요소가 하나만 있습니다.
  2. 각 공백 앞에는 가 붙습니다 \.

먼저 전체 명령을 변수에 넣고 그 기능이 무엇인지 확인합니다.

execcmd="convert -monitor ${filearray[@]} output.pdf"

예는 다음과 같습니다.

convert -monitor /tmp/pngpdf/file\ with\ spaces/1.png /tmp/pngpdf/file\ with\ spaces/2.png /tmp/pngpdf/file\ with\ spaces/3.png /tmp/pngpdf/file\ with\ spaces/4.png /tmp/pngpdf/file\ with\ spaces/5.png output.pdf

하지만 $execcmdConvert를 사용하여 실행하면 많은 오류가 발생합니다.

convert.im6: unable to open image `/tmp/pngpdf/file\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `/tmp/pngpdf/file\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `with\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `with\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `spaces/1.png': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: unable to open file `spaces/1.png' @ error/png.c/ReadPNGImage/3667.
convert.im6: unable to open image `/tmp/pngpdf/file\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `/tmp/pngpdf/file\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `with\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `with\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `spaces/2.png': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: unable to open file `spaces/2.png' @ error/png.c/ReadPNGImage/3667.
convert.im6: unable to open image `/tmp/pngpdf/file\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `/tmp/pngpdf/file\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `with\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `with\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `spaces/3.png': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: unable to open file `spaces/3.png' @ error/png.c/ReadPNGImage/3667.
convert.im6: unable to open image `/tmp/pngpdf/file\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `/tmp/pngpdf/file\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `with\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `with\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `spaces/4.png': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: unable to open file `spaces/4.png' @ error/png.c/ReadPNGImage/3667.
convert.im6: unable to open image `/tmp/pngpdf/file\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `/tmp/pngpdf/file\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `with\': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `with\' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `spaces/5.png': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: unable to open file `spaces/5.png' @ error/png.c/ReadPNGImage/3667.
convert.im6: no images defined `output.pdf' @ error/convert.c/ConvertImageCommand/3044.

분명히 내가하고 싶은 일을 올바르게 인식하지 못하는 것 같습니다. 백슬래시 자체가 이스케이프되므로 공백은 인수 구분 기능을 다시 얻습니다. 이 명령을 bash에 직접 입력하면 예상대로 원활하게 실행됩니다.

$ convert -monitor /tmp/pngpdf/file\ with\ spaces/1.png /tmp/pngpdf/file\ with\ spaces/2.png /tmp/pngpdf/file\ with\ spaces/3.png /tmp/pngpdf/file\ with\ spaces/4.png /tmp/pngpdf/file\ with\ spaces/5.png output.pdf
Load/Image//tmp/pngpdf/file with spaces[1.png]: 584 of 585, 100% complete
Load/Image//tmp/pngpdf/file with spaces[2.png]: 584 of 585, 100% complete
Load/Image//tmp/pngpdf/file with spaces[3.png]: 584 of 585, 100% complete
Load/Image//tmp/pngpdf/file with spaces[4.png]: 584 of 585, 100% complete
Load/Image//tmp/pngpdf/file with spaces[5.png]: 584 of 585, 100% complete
Mogrify/Image//tmp/pngpdf/file with spaces[5.png]: 4 of 5, 100% complete
resize image[output.pdf]: 180 of 181, 100% complete
resize image[output.pdf]: 180 of 181, 100% complete
resize image[output.pdf]: 180 of 181, 100% complete
resize image[output.pdf]: 180 of 181, 100% complete

이것은 나를 매우 혼란스럽게 하고 이 문제를 해결하는 방법을 모르겠습니다. 나는 또한 성공하지 못한 채 파일 배열에서 참조된 개별 파일 이름을 저장해 보았습니다 DEBUG: filearray: "/tmp/pngpdf/file with spaces/1.png" "/tmp/pngpdf/file with spaces/2.png" "/tmp/pngpdf/file with spaces/3.png" "/tmp/pngpdf/file with spaces/4.png" "/tmp/pngpdf/file with spaces/5.png". bash도 이러한 특수 문자를 이스케이프할 수 있습니다. 나는 이것이 bash의 작동 방식과 관련이 있을 것이라고 확신하지만, 이것을 스스로 알아낼 만큼 충분히 이해하지 못하는 것 같습니다. 그래서 누군가가 나에게 깨달음을 준다면 나는 매우 기쁠 것입니다. :)

추신: 저는 여러 곳에서 제 질문에 대한 답을 찾고 있었습니다(파일 이름에 공백이 포함된 쉘 스크립트 문제,생성된 파일 이름 목록을 인수 목록으로 사용 - 공백 포함,이름에 공백이 있는 파일을 반복하시겠습니까?,공백이 포함된 Bash 스크립트 및 파일,공백이나 기타 특수 문자 때문에 쉘 스크립트가 멈추는 이유는 무엇입니까?,파일 이름에 공백이 있는 스크립트 문제). 안타깝게도 거기에서는 내 질문에 대한 답을 찾을 수 없습니다. 어쩌면 너무 늦었을 수도 있습니다 ;)

답변1

execcmd="convert -monitor ${filearray[@]} 출력.pdf"

하지만 $execcmdConvert로 실행하면 오류가 많이 발생합니다.

$execcmd파일 이름과 파일 이름의 공백으로 구분된 부분 간의 구별이 이미 손실되었기 때문에 호출하지 마세요 . 대신 인용된 인수를 사용하여 명령 자체를 실행하십시오.

    convert -monitor "${filearray[@]}" output.pdf

관련 정보