tar 排除 ksh 中带单引号的参数

发布于 2024-11-29 09:59:11 字数 492 浏览 1 评论 0原文

我对以下 ksh 脚本有问题:

#! /usr/bin/ksh 

EXCLUDE+=" --exclude '/bin/a*' "
echo $EXCLUDE
tar --preserve-permissions --create  $EXCLUDE --file tar.tar /bin

由于将模式传递到排除目标,因此需要在其周围加上单引号。 (我想排除 /bin 文件夹中以“a”开头的所有文件)。

Echo 线还显示正确且未更改的结果(模式没有扩展并且单引号仍然存在)。然而,这个排除目标没有效果。

但如果我输入实际值而不是 $EXCLUDE,它就起作用了!

#! /usr/bin/ksh 

tar --preserve-permissions --create  --exclude '/bin/a*' --file tar.tar /bin

这一定是我遇到的一些引用问题,但是什么?

I have a problem with the following ksh script:

#! /usr/bin/ksh 

EXCLUDE+=" --exclude '/bin/a*' "
echo $EXCLUDE
tar --preserve-permissions --create  $EXCLUDE --file tar.tar /bin

Since passing in a pattern to the exclude target need single quotes around it. (I want to exclude all files in /bin folder beginning with "a").

The Echo line also show the correct and unchanged result (the pattern didn't expand and the single quote is still there). However this exclude target has no effect.

But if I put the actual value instead of $EXCLUDE, it worked!

#! /usr/bin/ksh 

tar --preserve-permissions --create  --exclude '/bin/a*' --file tar.tar /bin

It must be some quoting issue I'm encountering, but what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-12-06 09:59:11

EXCLUDE=' --exclude /bin/a* ' 就足够了。
您的代码排除了 '/bin/a*' (引号 '' 是模式的一部分)模式,但您真正想要的是排除 /bin/a * 模式。

EXCLUDE=' --exclude /bin/a* ' is enough.
Your code excludes the '/bin/a*' (quotes'' are part of pattern) pattern but what you actually want is to exclude /bin/a* pattern.

迷雾森÷林ヴ 2024-12-06 09:59:11

摘要

是的,您的问题与引用有关。

讨论

在调试此类问题时,我发现每行打印一个参数的 echo 变体非常有用。我使用的名称是 al (“参数列表”)。将其用作脚本中 tar 命令的前缀,使用“ksh -x tartest.ksh”运行,跟踪输出为:

+ EXCLUDE+=

注意 shell 的运行情况相当长的长度来确保 tar 命令(或 al 命令)可以看到单引号。当您遵循 John 的建议时,您会看到不同的输出:

+ EXCLUDE+=' --exclude /bin/a* '
+ echo --exclude '/bin/a*'
--exclude /bin/a*
+ al tar --preserve-permissions --create --exclude '/bin/a*' --file tar.tar /bin
tar
--preserve-permissions
--create
--exclude
/bin/a*
--file
tar.tar
/bin

请注意,这一次,tar (al) 准确地看到 /bin/a* 而不是 '/bin/a*'


您可以用 C 或 shell 简单地编写 al

for arg; do echo "$arg"; done

#include <stdio.h>
int main(int argc, char **argv)
{
    while (*++argv != 0)
        puts(*argv);
    return 0;
}

使用 /bin/ksh 在 MacOS X 10.7 上执行测试,其将自身标识为:

$ ksh --version
  version         sh (AT&T Research) 1993-12-28 s+
$
--exclude \'/bin/a*\' ' + echo --exclude

注意 shell 的运行情况相当长的长度来确保 tar 命令(或 al 命令)可以看到单引号。当您遵循 John 的建议时,您会看到不同的输出:


请注意,这一次,tar (al) 准确地看到 /bin/a* 而不是 '/bin/a*'


您可以用 C 或 shell 简单地编写 al



使用 /bin/ksh 在 MacOS X 10.7 上执行测试,其将自身标识为:


\'/bin/a*\''
--exclude '/bin/a*'
+ al tar --preserve-permissions --create --exclude 

注意 shell 的运行情况相当长的长度来确保 tar 命令(或 al 命令)可以看到单引号。当您遵循 John 的建议时,您会看到不同的输出:


请注意,这一次,tar (al) 准确地看到 /bin/a* 而不是 '/bin/a*'


您可以用 C 或 shell 简单地编写 al



使用 /bin/ksh 在 MacOS X 10.7 上执行测试,其将自身标识为:


\'/bin/a*\'' --file tar.tar /bin
tar
--preserve-permissions
--create
--exclude
'/bin/a*'
--file
tar.tar
/bin

注意 shell 的运行情况相当长的长度来确保 tar 命令(或 al 命令)可以看到单引号。当您遵循 John 的建议时,您会看到不同的输出:

请注意,这一次,tar (al) 准确地看到 /bin/a* 而不是 '/bin/a*'


您可以用 C 或 shell 简单地编写 al


使用 /bin/ksh 在 MacOS X 10.7 上执行测试,其将自身标识为:

Summary

Yes, your problem is related to quoting.

Discussion

When debugging this sort of issue, I find that a variant of echo that prints its arguments one per line is invaluable. The name I use for it is al (for 'argument list'). With that used as a prefix to the tar command in your script, run using 'ksh -x tartest.ksh', the trace output is:

+ EXCLUDE+=

Note how the shell has gone to considerable lengths to ensure that the tar command (or the al command) sees the single quotes. When you follow John's advice, you see a different output:

+ EXCLUDE+=' --exclude /bin/a* '
+ echo --exclude '/bin/a*'
--exclude /bin/a*
+ al tar --preserve-permissions --create --exclude '/bin/a*' --file tar.tar /bin
tar
--preserve-permissions
--create
--exclude
/bin/a*
--file
tar.tar
/bin

Note that this time, tar (al) sees exactly /bin/a* and not '/bin/a*'.


You can write al trivially in C or shell:

for arg; do echo "$arg"; done

#include <stdio.h>
int main(int argc, char **argv)
{
    while (*++argv != 0)
        puts(*argv);
    return 0;
}

Testing performed on MacOS X 10.7 with /bin/ksh, which identifies itself as:

$ ksh --version
  version         sh (AT&T Research) 1993-12-28 s+
$
--exclude \'/bin/a*\' ' + echo --exclude

Note how the shell has gone to considerable lengths to ensure that the tar command (or the al command) sees the single quotes. When you follow John's advice, you see a different output:


Note that this time, tar (al) sees exactly /bin/a* and not '/bin/a*'.


You can write al trivially in C or shell:



Testing performed on MacOS X 10.7 with /bin/ksh, which identifies itself as:


\'/bin/a*\''
--exclude '/bin/a*'
+ al tar --preserve-permissions --create --exclude 

Note how the shell has gone to considerable lengths to ensure that the tar command (or the al command) sees the single quotes. When you follow John's advice, you see a different output:


Note that this time, tar (al) sees exactly /bin/a* and not '/bin/a*'.


You can write al trivially in C or shell:



Testing performed on MacOS X 10.7 with /bin/ksh, which identifies itself as:


\'/bin/a*\'' --file tar.tar /bin
tar
--preserve-permissions
--create
--exclude
'/bin/a*'
--file
tar.tar
/bin

Note how the shell has gone to considerable lengths to ensure that the tar command (or the al command) sees the single quotes. When you follow John's advice, you see a different output:

Note that this time, tar (al) sees exactly /bin/a* and not '/bin/a*'.


You can write al trivially in C or shell:


Testing performed on MacOS X 10.7 with /bin/ksh, which identifies itself as:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文