Tar 在 bash 中的每个反斜杠前面添加了额外的反斜杠

托马斯豪瑟

我有一个数组$files,其中包含我想一起压缩的文件地址。我不喜欢这样写道:tar czf "output.tgz" "${files[@]}"这一切都有效,直到有一个文件名中带有空格。为此,我使用path="${path// /'\ '}"which 替换 all <space>\<space>然后我将像这样将它添加files+=("<space>$path");$files数组中。

当我尝试仅使用一个名为 的测试文件来执行此操作时main and space.c$files正确设置为:/home/tom/Desktop/main\ and\ space.c当我echo "Files array looks like this: $files"在tarring 之前调用时,但是当程序在下一行进行tarring 时,我收到错误消息:

tar: /home/tom/Desktop/main\\ and\\ space.c: 无法统计: 没有这样的文件或目录

我可以让 tar 避免在那里放置额外的反斜杠吗?

切普纳

您首先是在与您的阵列作斗争。只需将正确引用的名称添加到数组中;shell 会为你处理数组的内容。

$ files=()
$ files+=("test 1.txt")
$ files+=("test 2.txt")
$ files+=("test 3.txt" "test 4.txt")
$ tar czf output.tgz "${files[@]}"
$ tar tzf output.tgz
test 1.txt
test 2.txt
test 3.txt
test 4.txt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章