Can't quote a Cmd with equals sign correctly

moble
Fri, 06 Aug 2021 19:22:23 +0000
General Usage

I’ve been happily using DrWatson for my latest project, using its convention for filenames. For example, if my simulation uses parameters Dict(:a=>2, :b=>7), my output data file might be named simulation_a=2_b=7.h5.

But now the problem comes when I need to call a shell command from julia with one of these files. For example, I might want to run a shell command that is equivalently given by any of the following three options:

h5ls "simulation_a=2_b=7.h5"
h5ls 'simulation_a=2_b=7.h5'
h5ls simulation_a\=2_b\=7.h5

Either the filename itself should be surrounded by exactly one set of single or double quotes or each = should be escaped — not both quotes and escapes, and not two kinds of quoting.

The problem is when I try to make a Cmd to do these things; julia appears to be too clever for me, giving me either no quoting and no escaping, or quoting and escaping. These are some of the options I’ve tried (all of which fail):

julia> `h5ls simulation_a\=2_b\=7.h5`
`h5ls simulation_a=2_b=7.h5`

julia> `h5ls 'simulation_a=2_b=7.h5'`
`h5ls simulation_a=2_b=7.h5`

julia> `h5ls "simulation_a=2_b=7.h5"`
`h5ls simulation_a=2_b=7.h5`

julia> `h5ls "simulation_a\=2_b\=7.h5"`
`h5ls 'simulation_a\=2_b\=7.h5'`

julia> `h5ls "simulation_a\\=2_b\\=7.h5"`
`h5ls 'simulation_a\=2_b\=7.h5'`

julia> `h5ls simulation_a\\=2_b\\=7.h5`
`h5ls 'simulation_a\=2_b\=7.h5'`

julia> Cmd(["h5ls", "simulation_a=2_b=7.h5"])
`h5ls simulation_a=2_b=7.h5`

julia> Cmd(["h5ls", "'simulation_a=2_b=7.h5'"])
`h5ls "'simulation_a=2_b=7.h5'"`

julia> Cmd(["h5ls", "simulation_a\\=2_b\\=7.h5"])
`h5ls 'simulation_a\=2_b\=7.h5'`

Is there any way to achieve my goal?

(To make matters worse, I actually want to pass an array of files to a shell command…)

7 posts - 5 participants