巴什逃避空格的文件名,变量 [英] Bash escaping spaces in filename, in variable
问题描述
我很新来砸所以这可能是一些小事,但我只是没有得到它。我试图逃跑内部文件名的空间。看一看。请注意,这是一个工作的例子' - 我得到的交织与空白页的文件可能会实现比较容易,但我在这里关于空间
I'm quite new to Bash so this might be something trivial, but I'm just not getting it. I'm trying to escape the spaces inside filenames. Have a look. Note that this is a 'working example' - I get that interleaving files with blank pages might be accomplished easier, but I'm here about the space.
#! /bin/sh
first=true
i=combined.pdf
o=combined2.pdf
for f in test/*.pdf
do
if $first; then
first=false
ifile=\"$f\"
else
ifile=$i\ \"$f\"
fi
pdftk $ifile blank.pdf cat output $o
t=$i
i=$o
o=$t
break
done
说我有一个名为我file.pdf
文件(中间有空格)。我想用ifile变量包含字符串 combined.pdf我file.pdf
,这样PDFTK是能够使用它作为两个文件的参数 - 第一个是 combined.pdf
,和第二个是我file.pdf
。
Say I have a file called my file.pdf
(with a space). I want the ifile variable to contain the string combined.pdf "my file.pdf"
, such that pdftk is able to use it as two file arguments - the first one being combined.pdf
, and the second being my file.pdf
.
我试过逃跑(带或不带先逃离自己的报价等)的各种方法,但它一直劈我的
和 file.pdf
执行PDFTK时。
I've tried various ways of escaping (with or without first escaping the quotes themselves, etc.), but it keeps splitting my
and file.pdf
when executing pdftk.
编辑:为了澄清:我想在一个变量传递多个文件名(如多个参数)将PDFTK命令。我想它识别两个文件名之间的差别,而不是在空间撕裂一个文件名。
To clarify: I'm trying to pass multiple file names (as multiple arguments) in one variable to the pdftk command. I would like it to recognise the difference between two file names, but not tear one file name apart at the spaces.
推荐答案
把多个参数到一个单一变量就没有意义了。相反,把它们放到一个数组:
Putting multiple arguments into a single variable doesn't make sense. Instead, put them into an array:
args=(combined.pdf "my file.pdf");
注意我的file.pdf
是引用到preserve空白。
Notice that "my file.pdf"
is quoted to preserve whitespace.
您可以使用数组是这样的:
You can use the array like this:
pdftk "${args[@]}" ...
这将通过两个独立的参数 PDFTK
。在报价$ {ARGS [@]}
是必需的,因为他们告诉shell将每个数组元素视为一个单独的字(即不拆数组元素,即使它们包含空格)。
This will pass two separate arguments to pdftk
. The quotes in "${args[@]}"
are required because they tell the shell to treat each array element as a separate "word" (i.e. do not split array elements, even if they contain whitespace).
作为一个方面说明,如果你使用庆典
如数组主义,你的家当改为
As a side note, if you use bash
isms like arrays, change your shebang to
#!/bin/bash
这篇关于巴什逃避空格的文件名,变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!