命令中间有空格的文件名(不是 while/for 循环)
Filenames with spaces in the middle of a command (not a while/for loop)
我正在尝试向 pdftk 提供带空格的文件名列表(将 3 个 PDF 合并为一个 PDF),但它似乎不喜欢我的尝试。
# pdftk INPUT1.pdf INPUT2.pdf output OUTPUTFILE.pdf
它可以手动指定文件:
$ pdftk "pdf 1.pdf" "pdf 2.pdf" output t.pdf
$ ls t.pdf
t.pdf (and it works/opens fine)
但以下 none 似乎有效:
$ pdftk "$(ls --quoting-style=c | tr "\n" " ")" output t.pdf
Error: Unable to find file.
Error: Failed to open PDF file:
"pdf 1.pdf" "pdf 2.pdf" "pdf 3.pdf"
Errors encountered. No output created.
Done. Input errors, so no output created.
(注意上面,不在 $(ls) 周围使用引号也会失败)
或者这个:
$ ls -1
'pdf 1.pdf'
'pdf 2.pdf'
'pdf 3.pdf'
$ pdftk "$(ls -1 | tr "\n" " ")" output t.pdf
Error: Unable to find file.
Error: Failed to open PDF file:
pdf 1.pdf pdf 2.pdf pdf 3.pdf
Errors encountered. No output created.
Done. Input errors, so no output created.
考虑到您一次只想合并 3 个文件,您可以尝试以下操作,我已经在 Ubuntu 系统上对 3 个文件进行了测试(其中它也使用 space 处理文件名).你可以测试一次。
pdftk *.pdf output t.pdf
$ pdftk "$(some command)" output t.pdf
这会运行一些命令,然后双引号将输出封装到一个 "word" 中。无论命令是什么,结果都将是 pdftk
接收到一个参数,该参数包含命令的全部输出。
解析 ls
通常被认为是一个坏主意。
如@RavinderSingh13 所建议,只需指定 *.pdf
(甚至 *
)即可解决您的问题。 shell执行"globbing"展开通配符,自己正确拆分成"words":
$ pdftk * output t.pdf
如果你是 运行 除了 ls
之外的某个输出文件名列表的命令,你需要小心一点,以确保在正确的位置引用。
的 "Application Usage" 部分给出了一些可移植的方法(对命令输出有一些假设)
在具有 GNU 版本的实用程序(例如 find
和 xargs
)的系统上,使用非标准的 -print0
、-0
和相关的机制提供了更强大的机制假定 NULL 字符是用于分隔命令输出项的(唯一)特殊字符的命令参数。
我正在尝试向 pdftk 提供带空格的文件名列表(将 3 个 PDF 合并为一个 PDF),但它似乎不喜欢我的尝试。
# pdftk INPUT1.pdf INPUT2.pdf output OUTPUTFILE.pdf
它可以手动指定文件:
$ pdftk "pdf 1.pdf" "pdf 2.pdf" output t.pdf
$ ls t.pdf
t.pdf (and it works/opens fine)
但以下 none 似乎有效:
$ pdftk "$(ls --quoting-style=c | tr "\n" " ")" output t.pdf
Error: Unable to find file.
Error: Failed to open PDF file:
"pdf 1.pdf" "pdf 2.pdf" "pdf 3.pdf"
Errors encountered. No output created.
Done. Input errors, so no output created.
(注意上面,不在 $(ls) 周围使用引号也会失败)
或者这个:
$ ls -1
'pdf 1.pdf'
'pdf 2.pdf'
'pdf 3.pdf'
$ pdftk "$(ls -1 | tr "\n" " ")" output t.pdf
Error: Unable to find file.
Error: Failed to open PDF file:
pdf 1.pdf pdf 2.pdf pdf 3.pdf
Errors encountered. No output created.
Done. Input errors, so no output created.
考虑到您一次只想合并 3 个文件,您可以尝试以下操作,我已经在 Ubuntu 系统上对 3 个文件进行了测试(其中它也使用 space 处理文件名).你可以测试一次。
pdftk *.pdf output t.pdf
$ pdftk "$(some command)" output t.pdf
这会运行一些命令,然后双引号将输出封装到一个 "word" 中。无论命令是什么,结果都将是 pdftk
接收到一个参数,该参数包含命令的全部输出。
解析 ls
通常被认为是一个坏主意。
如@RavinderSingh13 所建议,只需指定 *.pdf
(甚至 *
)即可解决您的问题。 shell执行"globbing"展开通配符,自己正确拆分成"words":
$ pdftk * output t.pdf
如果你是 运行 除了 ls
之外的某个输出文件名列表的命令,你需要小心一点,以确保在正确的位置引用。
在具有 GNU 版本的实用程序(例如 find
和 xargs
)的系统上,使用非标准的 -print0
、-0
和相关的机制提供了更强大的机制假定 NULL 字符是用于分隔命令输出项的(唯一)特殊字符的命令参数。