create command with spaces without quoting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

create command with spaces without quoting

Andre Bieler
I want to generate a command for imagemagick and use options loaded from a file.
So the number of utilized options is not known beforehand.

An example might look like:

convert -density 300 somefile.pdf -depth 10 somefile.jpeg

It is easy to generate the string above, but when interpolating it
into a command it gets quoted because of the spaces.

So

s = "convert -density 300 somefile.pdf -depth 10 somefile.jpeg"
run(`$s`)

does not work. Is there an easy way to omit quoting?
Or how to generate the command when having vectors of keyword : value pairs
such as

kw = ["-density", "-depth"]
vals = [300, 8]

(assuming for now that all these options simply follow each other in one place of
the command)

Thanks for the help.

Andre
Reply | Threaded
Open this post in threaded view
|

Re: create command with spaces without quoting

Yichao Yu
On Thu, Nov 3, 2016 at 12:17 PM, Andre Bieler <[hidden email]> wrote:

> I want to generate a command for imagemagick and use options loaded from a
> file.
> So the number of utilized options is not known beforehand.
>
> An example might look like:
>
> convert -density 300 somefile.pdf -depth 10 somefile.jpeg
>
> It is easy to generate the string above, but when interpolating it
> into a command it gets quoted because of the spaces.
>
> So
>
> s = "convert -density 300 somefile.pdf -depth 10 somefile.jpeg"
> run(`$s`)
>
> does not work. Is there an easy way to omit quoting?
> Or how to generate the command when having vectors of keyword : value pairs
> such as
>
> kw = ["-density", "-depth"]
> vals = [300, 8]
>
> (assuming for now that all these options simply follow each other in one
> place of
> the command)
>
> Thanks for the help.

Interpolate in the command and not the string

you can do `command $option` where option is a string or an array of string.

>
> Andre
Reply | Threaded
Open this post in threaded view
|

Re: create command with spaces without quoting

Stefan Karpinski
Interpolating commands into commands is a useful pattern as well:

cmd = `convert`
if haskey(opts, :density)
    cmd = `$cmd -density $(opts[:density])`
end
if haskey(opts, :depth)
    cmd = `$cmd -depth $(opts[:depth])`
end
run(cmd)



On Thu, Nov 3, 2016 at 12:21 PM, Yichao Yu <[hidden email]> wrote:
On Thu, Nov 3, 2016 at 12:17 PM, Andre Bieler <[hidden email]> wrote:
> I want to generate a command for imagemagick and use options loaded from a
> file.
> So the number of utilized options is not known beforehand.
>
> An example might look like:
>
> convert -density 300 somefile.pdf -depth 10 somefile.jpeg
>
> It is easy to generate the string above, but when interpolating it
> into a command it gets quoted because of the spaces.
>
> So
>
> s = "convert -density 300 somefile.pdf -depth 10 somefile.jpeg"
> run(`$s`)
>
> does not work. Is there an easy way to omit quoting?
> Or how to generate the command when having vectors of keyword : value pairs
> such as
>
> kw = ["-density", "-depth"]
> vals = [300, 8]
>
> (assuming for now that all these options simply follow each other in one
> place of
> the command)
>
> Thanks for the help.

Interpolate in the command and not the string

you can do `command $option` where option is a string or an array of string.

>
> Andre

Reply | Threaded
Open this post in threaded view
|

Re: create command with spaces without quoting

Andre Bieler
Excellent, thanks for both suggestions.