Kre*_*uvf 1 bash path whitespace escape-characters
在 bash 脚本中,我将 PDF 逐页光栅化为单个文件,最后生成的单个 PNG 再次合并到 PDF 中,如下所示:
\n\nconvert -monitor /path/to/1.png /path/to/2.png /path/to/3.png ... output.pdf\nRun Code Online (Sandbox Code Playgroud)\n\n脚本仍然存在的唯一问题是无法正确处理带有空格的文件。以下是我尝试过的一些事情:
\n\nnewfile=$(sed -r -e \'s| |\\\\ |g\' <<< "$tmppath$curr.png")\necho "DEBUG: newfiles : $newfiles"\nfilearray[$curr-1]="$newfiles"\necho "DEBUG: filearray: ${filearray[*]}"\nRun Code Online (Sandbox Code Playgroud)\n\n这产生(每页/文件):
\n\nDEBUG: newfiles : /tmp/pngpdf/file\\ with\\ spaces/1.png\nDEBUG: filearray: /tmp/pngpdf/file\\ with\\ spaces/1.png\nRun Code Online (Sandbox Code Playgroud)\n\n后来,我有两条调试消息
\n\necho "DEBUG: filearray: ${filearray[*]}"\necho "DEBUG: ${filearray[0]}, ${filearray[1]}, ${filearray[2]}, ..."\nRun Code Online (Sandbox Code Playgroud)\n\n查看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\nDEBUG: /tmp/pngpdf/file\\ with\\ spaces/1.png, /tmp/pngpdf/file\\ with\\ spaces/2.png, /tmp/pngpdf/file\\ with\\ spaces/3.png, ...\nRun Code Online (Sandbox Code Playgroud)\n\n我可以清楚地看到以下内容:
\n\n\\.我首先将整个命令放入一个变量中,看看会执行什么:
\n\nexeccmd="convert -monitor ${filearray[@]} output.pdf"\nRun Code Online (Sandbox Code Playgroud)\n\n一个例子可能看起来像这样:
\n\nconvert -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\nRun Code Online (Sandbox Code Playgroud)\n\n但是使用$execcmdConvert 执行它会向我抛出许多错误:
convert.im6: unable to open image `/tmp/pngpdf/file\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `/tmp/pngpdf/file\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `with\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `with\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `spaces/1.png\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: unable to open file `spaces/1.png\' @ error/png.c/ReadPNGImage/3667.\nconvert.im6: unable to open image `/tmp/pngpdf/file\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `/tmp/pngpdf/file\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `with\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `with\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `spaces/2.png\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: unable to open file `spaces/2.png\' @ error/png.c/ReadPNGImage/3667.\nconvert.im6: unable to open image `/tmp/pngpdf/file\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `/tmp/pngpdf/file\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `with\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `with\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `spaces/3.png\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: unable to open file `spaces/3.png\' @ error/png.c/ReadPNGImage/3667.\nconvert.im6: unable to open image `/tmp/pngpdf/file\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `/tmp/pngpdf/file\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `with\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `with\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `spaces/4.png\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: unable to open file `spaces/4.png\' @ error/png.c/ReadPNGImage/3667.\nconvert.im6: unable to open image `/tmp/pngpdf/file\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `/tmp/pngpdf/file\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `with\\\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: no decode delegate for this image format `with\\\' @ error/constitute.c/ReadImage/544.\nconvert.im6: unable to open image `spaces/5.png\': No such file or directory @ error/blob.c/OpenBlob/2638.\nconvert.im6: unable to open file `spaces/5.png\' @ error/png.c/ReadPNGImage/3667.\nconvert.im6: no images defined `output.pdf\' @ error/convert.c/ConvertImageCommand/3044.\nRun Code Online (Sandbox Code Playgroud)\n\n显然,它无法正确识别我想要做什么。反斜杠本身被转义,因此,空格重新获得了它们的参数界定能力。当直接将该命令放入 bash 时,它运行顺利(如预期):
\n\n$ 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\nLoad/Image//tmp/pngpdf/file with spaces[1.png]: 584 of 585, 100% complete\nLoad/Image//tmp/pngpdf/file with spaces[2.png]: 584 of 585, 100% complete\nLoad/Image//tmp/pngpdf/file with spaces[3.png]: 584 of 585, 100% complete\nLoad/Image//tmp/pngpdf/file with spaces[4.png]: 584 of 585, 100% complete\nLoad/Image//tmp/pngpdf/file with spaces[5.png]: 584 of 585, 100% complete\nMogrify/Image//tmp/pngpdf/file with spaces[5.png]: 4 of 5, 100% complete\nresize image[output.pdf]: 180 of 181, 100% complete\nresize image[output.pdf]: 180 of 181, 100% complete\nresize image[output.pdf]: 180 of 181, 100% complete\nresize image[output.pdf]: 180 of 181, 100% complete\nRun Code Online (Sandbox Code Playgroud)\n\n这让我非常困惑,我不知道如何解决这个问题。我也尝试保存 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"效果。bash 可能也会转义这些特殊字符。我确信这一定与 bash 的工作原理有关,但似乎我的理解还不足以自行解决这个问题。因此,如果有人能够启发我,我会非常高兴。:)
PS:我一直在几个地方寻找我的问题的答案(Shell script issues with filenames contains space,Using a generated list of filenames as argument list \xe2\x80\x94 with space,循环遍历带有空格的文件名称?、Bash 脚本和其中包含空格的文件、为什么我的 shell 脚本会因空格或其他特殊字符而阻塞?、文件名中包含空格的脚本出现问题)。不幸的是,我无法在那里找到我的问题的答案。也许已经太晚了;)
\nexeccmd="convert -monitor ${filearray[@]} 输出.pdf"
但是用
$execcmdConvert 执行它会给我带来很多错误
不要调用$execcmd,因为它已经失去了文件名和文件名的空格分隔部分之间的区别。相反,使用带引号的参数执行命令本身:
convert -monitor "${filearray[@]}" output.pdf
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
618 次 |
| 最近记录: |