2

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

Gary
  • 2,393
  • 12
  • 31
  • `$'...'` is a [type of quote in bash](https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html). They shouldn't be included literally in the arguments to the program you're trying to run. – Shawn May 11 '23 at 02:20
  • Use double quotes around the whole `--output=...` argument in tcl? That should handle the newline escape. – Shawn May 11 '23 at 02:21
  • @Shawn Thank you very much. As you wrote, this worked: `[exec mediainfo "--Output=General;File Name: %FileName%\nAudio; Duration: %Duration/String3%" 01_gen/01001.mp3]` – Gary May 11 '23 at 02:25
  • I know nothing about bash and had not heard of `medainfo` until last night, but the `\n` is needed, apparently, to be able to access two different sections of properties. I didn't add it for styling the output. I was trying to make use of two examples at [SO quest](https://stackoverflow.com/questions/41231998/mediainfo-cli-command-line-interface-syntax-teaching-me-once-for-all). I probably should have paid closer attention to the first answer rather than the second. – Gary May 11 '23 at 02:31
  • Using braces and a literal newline inside them works like in your edit works too; just kind of ugly IMHO. – Shawn May 11 '23 at 02:53

1 Answers1

4

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.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • When I have an argument like that, I'm inclined to generate it in a variable first, or at least the parts with complex structure (I also like to do input filenames like that). Then it gets passed as a simple `exec mediainfo --Output=$outputFormat $inputFile`. – Donal Fellows May 12 '23 at 10:21