tar 排除 ksh 中带单引号的参数
我对以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.摘要
是的,您的问题与引用有关。
讨论
在调试此类问题时,我发现每行打印一个参数的
echo
变体非常有用。我使用的名称是al
(“参数列表”)。将其用作脚本中tar
命令的前缀,使用“ksh -x tartest.ksh
”运行,跟踪输出为:注意 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 isal
(for 'argument list'). With that used as a prefix to thetar
command in your script, run using 'ksh -x tartest.ksh
', the trace output is:Note how the shell has gone to considerable lengths to ensure that the
tar
command (or theal
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: