简体   繁体   English

在bash脚本中转义空间

[英]Escaping space in bash script

I am trying to make a script to append all files ending with .hash to be verified by md5deep. 我正在尝试创建一个脚本来附加以.hash结尾的所有文件,以便由md5deep验证。 Files with space in their name seem to break this script. 名称中包含空格的文件似乎会破坏此脚本。

#!/bin/bash
XVAR=""
for f in *.hash
do
    XVAR="$XVAR -x $f "
done
md5deep -e $XVAR -r *

Whenever i run the script with a file called "O S.hash" i would get 每当我使用名为“O S.hash”的文件运行脚本时,我都会得到

O: No such file or directory

If i change XVAR="$XVAR -x $f " to XVAR="$XVAR -x \\'$f\\' " or XVAR="$XVAR -x \\"$f\\" " 如果我将XVAR="$XVAR -x $f "更改为XVAR="$XVAR -x \\'$f\\' "XVAR="$XVAR -x \\"$f\\" "

md5deep will interpenetrate the input as "O instead md5deep会将输入互相渗透为“O”

"O: No such file or directory

an echo of the variable in the script shows XVAR as -x 'O S.hash' or -x "O S.hash" 脚本中变量的回显显示XVAR为-x 'O S.hash'-x "O S.hash"

a manual input of the command in shell such as md5deep -e -x "O S.hash" -r * works but if its in the script the command seems to break 在shell中手动输入命令,例如md5deep -e -x "O S.hash" -r *但如果它在脚本中命令似乎破坏了

This is not the nicest solution, but is seems it will work: 这不是最好的解决方案,但似乎它会起作用:

find . -name '*.hash' -printf "-x\0%p\0" | xargs -0 md5deep -r * -e

This actually doesn't do exactly the same as the OP wanted, so here's a modification as suggested by Tim Pote and Jonathan Leffler: 这实际上与OP想要的完全不同,所以这是Tim Pote和Jonathan Leffler建议的修改:

find . -maxdepth 1 -name '*.hash' -printf "-x\0%p\0" | xargs -0 md5deep -r * -e

Now you know why people on Unix systems traditionally avoided file names with spaces in them (and directory names likewise) — it is a nuisance (to be polite about it) to have to program the shell to handle such names. 现在你知道为什么Unix系统上的人传统上避免使用带空格的文件名(以及同样的目录名) - 必须对shell进行编程以处理这些名称是一件令人讨厌的事情(对它有礼貌)。 The shell was designed for use in systems without such names. shell设计用于没有这些名称的系统。 Newlines also cause much grief. 换行也会引起很大的麻烦。

With bash , your best solution by far is to use an array to hold the elements, and then "${array[@]}" to list them; 使用bash ,到目前为止,您最好的解决方案是使用数组来保存元素,然后使用"${array[@]}"列出它们; it is almost trivial: 这几乎是微不足道的:

declare -a XVAR

for file in *.hash
do
    XVAR+=("-x" "$file")
done

md5deep -e "${XVAR[@]}" -r *

( Exploiting the array extension notation mentioned by Gordon Davisson . See section §6.7 'Arrays' of the bash reference manual (for Bash 4.1) for a lot of array information; see section §3.4 'Shell Parameters' for the += operator.) 利用Gordon Davisson提到的数组扩展表示法。有关大量数组信息,请参阅bash参考手册(适用于Bash 4.1)的第6.7节“数组”;有关+=运算符,请参见§3.4“Shell参数”一节。)

If you can't use arrays for some reason, then you need a program that escapes its arguments so that the shell won't distort things. 如果由于某种原因你不能使用数组,那么你需要一个程序来逃避它的参数,这样shell就不会扭曲东西。 I have such a program, called escape : 我有这样一个叫做escape的程序:

XVAR=
for file in *.hash
do
    name=$(escape "$file")
    XVAR="$XVAR -x $file"
done
eval md5deep -e $XVAR -r *

With the eval, it is tricky to use; 使用eval,使用起来很棘手; it works, but use arrays. 它工作,但使用数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM