处理exec命令中的单引号和$符号。

huangapple go评论66阅读模式
英文:

Handling single quotes and $ in exec command

问题

I'm here to assist with your translation request. Here's the translation of the provided text:

我试图执行这行代码,但在“Audio”后面添加了换行符,这只是为了不显示水平滚动条:

[exec mediainfo --Output=$'General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%' 01_gen/01001.mp3]

这在Linux命令行中运行,不需要exec,但在Tcl中无法运行。错误消息是:

Usage: "mediainfo [-Options...] FileName1 [Filename2...]"
"mediainfo --Help" 以显示更多信息

我了解到单引号对于Tcl来说“没有特殊意义”(在https://www.tcl.tk/man/tcl8.6/TclCmd/exec.html),并且那里的示例似乎用{}替换了它们。我也尝试了转义$,但生成相同的错误。

[exec mediainfo --Output=${General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%} 01_gen/01001.mp3]

我还尝试了转义分号,它不会出错,但返回的是语句中请求的所有值,而不仅仅是请求的两个。

[exec mediainfo --Output=${General\;File Name: %FileName%\nAudio\;
 Duration: %Duration/String3%} 01_gen/01001.mp3]

我还尝试了在这个SO问题中指出的扩展方法的几种变体,但我能做到的最好的是返回所有媒体属性,而不仅仅是两个请求的属性。

set cmd {mediainfo --Output=$'General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%' 01_gen/01001.mp3}
chan puts stdout [exec {*}$cmd]

请问我做错了什么,需要在Tcl中执行此操作?

谢谢。

编辑:

现在Shawn已经解释了问题,我意识到,如果我仔细阅读了关于mediainfo的SO问题的第一个答案(并且对于bash不是无知的话),我本可以显式添加新行,使其成为该答案所称的模板;然后只需使用普通的Tcl替换。有时候我可能相当迟钝。

set param {--Output=General;File Name: %FileName%
Audio; Duration: %Duration/String3%}
chan puts stdout [exec mediainfo $param 01_gen/01001.mp3]
# 文件名:01001 时长:00:07:31.422

希望这对您有所帮助。如果需要进一步解释或翻译,请告诉我。

英文:

I'm trying to execute this line of code, without the line break after "Audio" which is added, here, only to not show the horizontal scroll bar:

[exec mediainfo --Output=$'General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%' 01_gen/01001.mp3]

which runs on the command line in Linux without the exec; but it won't run in Tcl. The error is:

Usage: "mediainfo [-Options...] FileName1 [Filename2...]"
"mediainfo --Help" for displaying more information

I read about single quotes having "no special significance to Tcl" (at https://www.tcl.tk/man/tcl8.6/TclCmd/exec.html) and the example there appears to replace them with {}. And I tried escaping the $, but it generates the same error.

[exec mediainfo --Output=${General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%} 01_gen/01001.mp3]

I also tried escaping the semi-colons and it does not error, but it returns all the values rather than only those requested in the statement.

[exec mediainfo --Output=${General\;File Name: %FileName%\nAudio\;
 Duration: %Duration/String3%} 01_gen/01001.mp3]

I also tried a couple variations of the expansion method as pointed out in this SO question but the best I can get is a return of all the media properties rather than the two requested.

set cmd {mediainfo --Output=$'General;File Name: %FileName%\nAudio;
 Duration: %Duration/String3%' 01_gen/01001.mp3}
chan puts stdout [exec {*}$cmd]

Would you please explaing what I'm doing wrong and need to do to get this to execute in Tcl?

Thank you.

Edit:

Now that Shawn has explained the issue, I realize that, had I read the first answer at SO question about mediainfo more closely (and was not ignorant concerning bash) I could have added the new line explicitly, making what that answer refers to as a template; and then just used normal Tcl substitution. Sometimes I can be pretty dense.

set param {--Output=General;File Name: %FileName%
Audio; Duration: %Duration/String3%}
chan puts stdout [exec mediainfo $param 01_gen/01001.mp3]
# File Name: 01001 Duration: 00:07:31.422

答案1

得分: 4

你需要的是:

exec mediainfo "--Output=General;File Name: %FileName%\nAudio; Duration: %Duration/String3%" 01_gen/01001.mp3

在像bash这样的shell中(还有其他一些,但不包括POSIX sh),$'...'是一种引号类型,用于将\n转换为实际的换行字符,并将所有以空格分隔的部分组成单个参数;美元符号和单引号实际上不会传递给该参数中的程序。在tcl中,双引号具有类似的效果,但应该围绕整个参数,而不仅仅是部分。

英文:

You need:

exec mediainfo "--Output=General;File Name: %FileName%\nAudio; Duration: %Duration/String3%" 01_gen/01001.mp3

In a shell like bash (and a few others, but not POSIX sh), $'...' is a type of quote used here to convert the \n to an actual newline character and group all the space separated parts into a single argument; the dollar sign and single quotes aren't actually passed to the program in that argument. Double quotes in tcl have a similar effect, but should be around the entire argument, not just part of it.

huangapple
  • 本文由 发表于 2023年5月11日 09:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定