如何在BSD tar路径替换选项中转义字符 [英] How to escape chars in BSD tar path substitution option
问题描述
这不起作用:
$ tar -cf /tmp/z.tar -C /space/myroot -s '/^\.svn\/patches/__patches' src .svn/patches
tar: Invalid regular expression: trailing backslash (\)
也不好:
Also no good:
$ tar -cf /tmp/z.tar -C /space/myroot -s '/^\\.svn\\/patches/__patches' src .svn/patches
tar: Invalid replacement flag _
反斜杠(最多4个)和引号的每个组合都给了我这两个错误之一。但是我知道第一个在sed中起作用:
Every combination of backslashes (up to 4) and quoting gives me one of those two errors. However I know the first one works in sed:
$ tar -cf /tmp/z.tar -C /space/myroot src .svn/patches
$ tar -tf /tmp/z.tar | sed 's/^\.svn\/patches/__patches/'
src/a.c
src/b.c
__patches/A.patch
__patches/B.patch
我也知道我可以解决这个问题:
Also I know I can cop-out and do this:
$ tar -cf /tmp/z.tar -C /space/myroot -s '/^.svn.patches/__patches' src .svn/patches
-但我真的不想要。
-- but I don't really want to.
推荐答案
来自人bsdtar :
-s模式
....
其中old是基本正则表达式
...
-s pattern
....
where old is a basic regular expression
...
基本正则表达式应该与一起使用\ /
。不幸的是, bsdtar
在这里写错了,并且不能正确处理转义。
Basic regular expression should work with \/
. Sadly bsdtar
is incorrectly written here, and does not handle escaping correctly.
从 bsdtar / subst.c :
end_pattern = strchr(rule_text + 1, *rule_text);
rule_text
是字符串 char *
与您传递给 -s
选项的字符串一起传递,因此它具有字符串 / ^ s.svn\ / patches / __ patches
。 end_pattern
应该指向该模式的最后一个字符(即<$ c $中的 /
c> / __补丁)。但是,由于使用了简单的strchr,所以使用了找到的第一个 /
( \ /
中的字符)是否使用它。错误:
rule_text
is a string char *
which is passed with the string you pass to -s
option, so it has the string /^\.svn\/patches/__patches
. The end_pattern
should point to the last character of the pattern (ie. to the /
in from of /__patches
). However as simple strchr is used, so the first /
found is used (the character in \/
), escaped or not, it is used. The error:
tar: Invalid regular expression: trailing backslash (\)
来自 regcomp 尝试解析正则表达式 ^ \.svn\
(因为这是两个 / $ c之间的部分$ c>)。由于表达式中的尾部反斜杠
\
后面没有任何内容,因此会引发错误(并且正确-尾部有反斜杠)。可以考虑向bsdtar开发人员发布错误报告,但这可能真的不值得修复。
comes from regcomp trying to parse the regular expression ^\.svn\
(as this is the part stripped between the two /
). As there is nothing behind the trailing backslash \
in the expression, it throws an error (and is correct - a trailing backslash is there). One could think of posting a bug report to bsdtar developers, but it's probably not really worth fixing.
请注意, -s的语法
是 /旧/新/
。模式中缺少尾随的 /
,您将丢失。在最后一个 /
之后,您可以指定标志。
Note that the syntax for -s
is /old/new/
. There is a trailing /
in the pattern, which you are missing. After the last /
you can specify flags.
但是,您可以使用以下方法解决该问题:
You can however workaround the issue with:
bsdtar -s '#\.svn/patches#__patches#'
任何字符都可用于 /
。
这篇关于如何在BSD tar路径替换选项中转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!