Stream: helpdesk (published)

Topic: ✔ Quotation marks in commands


view this post on Zulip mbaz (Jul 13 2022 at 19:25):

I want to run exiv2 from Julia, but the command I need has very weird syntax: exiv2 -M"set ...". Julia transforms this to exiv2 '-M"set ..."', which fails to run.

Is there a way to force Julia not to quote those quotation marks?

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:29):

raw string literal?

view this post on Zulip mbaz (Jul 13 2022 at 21:34):

Nope -- still inserts the '.

julia> rsl = r"""-M"set ..." """
r"-M\"set ...\" "

julia> cmd = `exiv2 $rsl`
`exiv2 'r"-M\"set ...\" "'`

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:36):

You can use single quotes if there is nothing for the shell to interpolate

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:39):

You could also check how Julia itself parses its own flags, since it has julia -C"native,..."

view this post on Zulip mbaz (Jul 13 2022 at 21:42):

Single quotes don't work here; if I use them, Julia inserts double quotes. I'm not sure seeing how Julia parses those is useful here, since the problem is declaring the command, not parsing it.

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:43):

I see. My only thought is to construct a valid command in your shell, write the string to a file, and inspect the bytes to reproduce it in Julia.

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:43):

with a hex editor

view this post on Zulip mbaz (Jul 13 2022 at 21:44):

That's a good idea, thanks!

view this post on Zulip mbaz (Jul 13 2022 at 21:45):

But I wonder if I'm missing something, or if this is a limitation of Julia's command declaration syntax and opening an issue is warranted.

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:45):

No idea how useful that would be, but it may at least give you an idea of where the bytes differ and give some insight on how to correct them

view this post on Zulip Michael Fiano (Jul 13 2022 at 21:50):

Which OS are you on?

view this post on Zulip mbaz (Jul 13 2022 at 21:55):

Linux.

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:09):

Best I could come up with as a newbie:

run(setenv(`sh -c "echo \$TEST"`, ("TEST" => "exiv2 -M\"set foo\"",)));

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:10):

that will at least echo what you want using a shell subprocess

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:10):

No idea how to remove the envar

view this post on Zulip mbaz (Jul 13 2022 at 22:13):

Thanks for giving it a shot!

view this post on Zulip mbaz (Jul 13 2022 at 22:23):

Hm, exiv2 is not doing what it's supposed to do when I run it like this. Everything looks fine, though.

view this post on Zulip mbaz (Jul 13 2022 at 22:24):

Anyway, I found a workaround (I can write the command to a temporary file and point exiv2 to it). I think I'll open an issue, though; Julia should be able to handle this syntax.

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:26):

I see your frustration

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:26):

Cmd(["exiv2 -M\"set Xmp.dc.title lang=en-US Hello\" /home/mfiano/Temp/test.png"])

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:26):

I mean those damn single quotes

view this post on Zulip Michael Fiano (Jul 13 2022 at 22:45):

I just spent a half hour trying to figure it out. It seems Julia is trying to be too clever with its interpolation. Inserting a backslash to escape a double quote automatically places the whole string in single quotes, which is not a valid command.

view this post on Zulip mbaz (Jul 13 2022 at 23:16):

Yep, that's it.

view this post on Zulip Jakob Nybo Nissen (Jul 14 2022 at 09:37):

Please file an issue!

view this post on Zulip Sebastian Pfitzner (Jul 14 2022 at 11:29):

FWIW, in this specific case you can also call exiv2 with

julia> run(`exiv2 "-Mset Exif.Photo.UserComment My Photo 333" transport.png`)
Process(`exiv2 '-Mset Exif.Photo.UserComment My Photo 333' transport.png`, ProcessExited(0))

shell> exiv2 transport.png
...
Exif comment    : My Photo 333

view this post on Zulip Sebastian Pfitzner (Jul 14 2022 at 11:30):

actually, what's the command that's failing for you?

view this post on Zulip Sebastian Pfitzner (Jul 14 2022 at 11:31):

julia> run(`exiv2 -M"set Exif.Photo.UserComment My Photo3" transport.png`)
Process(`exiv2 '-Mset Exif.Photo.UserComment My Photo3' transport.png`, ProcessExited(0))

also works fine for me

view this post on Zulip mbaz (Jul 14 2022 at 13:58):

I think I was confused by Julia eliding the "s:

julia> `exiv2 -M"set Exif.Photo.UserComment My Photo3" image.jpg`
`exiv2 '-Mset Exif.Photo.UserComment My Photo3' image.jpg`

I assumed this would fail. So I tried this:

julia> run(`exiv2 -M\"set Exif.Photo.UserComment My Photo3\" image.jpg`)
-M option 1: Invalid command line:
exiv2: Error parsing -M option arguments
Usage: exiv2 [ options ] [ action ] file ...

Manipulate the Exif metadata of images.
ERROR: failed process: Process(`exiv2 '-M"set' Exif.Photo.UserComment My 'Photo3"' image.jpg`, ProcessExited(1)) [1]

and I think from here I went down a rabbit hole instead of just doing the simplest thing, running the original command :sweat_smile:

view this post on Zulip Sebastian Pfitzner (Jul 14 2022 at 13:59):

yeah, Cmd printing is very confusing

view this post on Zulip Sebastian Pfitzner (Jul 14 2022 at 13:59):

Julia tries to be too smart for its own good

view this post on Zulip Notification Bot (Jul 14 2022 at 13:59):

mbaz has marked this topic as resolved.


Last updated: Dec 28 2024 at 04:38 UTC