如何传递包含'<'的命令行参数“执行”

发布于 2024-11-29 23:30:57 字数 389 浏览 1 评论 0 原文

$ date > '< abcd'
$ cat '< abcd'
<something>
$ tclsh8.5
% exec cat {< abcd}
couldn't read file " abcd": no such file or directory

哎呀。这是由于“exec”的规范造成的。

如果一个 arg(或一对 arg)具有下述形式之一,则 exec 使用它来控制子进程之间的输入和输出流。此类参数不会传递给子进程。以诸如“< fileName”,fileName 可以位于与“<”不同的单独参数中或者在同一个参数中没有中间空格”。

有没有办法解决这个问题?

$ date > '< abcd'
$ cat '< abcd'
<something>
$ tclsh8.5
% exec cat {< abcd}
couldn't read file " abcd": no such file or directory

whoops. This is due to the the specification of 'exec'.

If an arg (or pair of args) has one of the forms described below then it is used by exec to control the flow of input and output among the subprocess(es). Such arguments will not be passed to the subprocess(es). In forms such as “< fileName”, fileName may either be in a separate argument from “<” or in the same argument with no intervening space".

Is there a way to work around this?

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

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

发布评论

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

评论(4

趴在窗边数星星i 2024-12-06 23:30:57

该值是否必须作为参数传递?如果没有,您可以使用类似以下的内容:

set strToPass "< foo"
exec someProgram << $strToPass

对于文件名,您可以(几乎总是)传递完全限定名称。完全限定名称可以通过文件规范化获得:

exec someProgram [file normalize "< foo"]       ;# Odd filename!

但是如果您需要传入一个参数,其中<(或> ;) 是第一个字符,你被卡住了。 exec 命令始终使用重定向等参数;与 Unix shell 不同,您不能仅使用引用来解决它。

但您可以使用帮助程序。因此,在 Unix 上你可以这样做:

exec /bin/sh -c "exec someProgram \"$strToPass\""

(子程序只是将其自身替换为你想要运行的内容,传递你真正想要的参数。你可能需要使用 string map regsub 将反斜杠放在有问题的元字符前面。)

在 Windows 上,您必须编写一个批处理文件并运行它,这有很多警告和令人讨厌的副作用,特别是对于 GUI 应用程序。

Does the value have to be passed as an argument? If not, you can use something like this:

set strToPass "< foo"
exec someProgram << $strToPass

For filenames, you can (almost always) pass the fully qualified name instead. The fully qualified name can be obtained with file normalize:

exec someProgram [file normalize "< foo"]       ;# Odd filename!

But if you need to pass in an argument where < (or >) is the first character, you're stuck. The exec command always consumes such arguments as redirections; unlike with the Unix shell, you can't just use quoting to work around it.

But you can use a helper program. Thus, on Unix you can do this:

exec /bin/sh -c "exec someProgram \"$strToPass\""

(The subprogram just replaces itself with what you want to run passing in the argument you really wanted. You might need to use string map or regsub to put backslashes in front of problematic metacharacters.)

On Windows, you have to write a batch file and run that, which has a lot of caveats and nasty side issues, especially for GUI applications.

往日 2024-12-06 23:30:57

一种简单的解决方案:确保单词不以重定向字符开头

exec cat "./< abcd"

一种稍微复杂一点的解决方案:

exec sh -c {cat '< abcd'}

# also
set f {< abcd}
exec sh -c "cat '$f'"

Tcl Wiki 上的页面稍微讨论了这个问题。

One simple solution: ensure the word does not begin with the redirection character:

exec cat "./< abcd"

One slightly more complex:

exec sh -c {cat '< abcd'}

# also
set f {< abcd}
exec sh -c "cat '$f'"

This page on the Tcl Wiki talks about the issue a bit.

哆啦不做梦 2024-12-06 23:30:57

你试过这个吗?

% exec {cat < abcd}

Have you tried this?

% exec {cat < abcd}
一城柳絮吹成雪 2024-12-06 23:30:57

尝试:

set myfile "< abcd"

exec cat $myfile

Try:

set myfile "< abcd"

exec cat $myfile

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