1

would like to run a system command on ruby using popen3 function from Open3. It would be something like:

pdf2htmlEX --zoom 1.3 ~/test.pdf

As the filename will be passed by params, I would like to sanitize it. if run, for example:

Open3.popen3("pdf2htmlEX --zoom 1.3 ~/test.pdf") do |stdin, stdout, stderr, wait_thr|

end

The code works fine, but when I try to separate the argument (as the functions suggests that way it would be escaped), something like:

Open3.popen3("pdf2htmlEX --zoom 1.3", "~/test.pdf") do |stdin, stdout, stderr, wait_thr|

end

it gives me the error: No such file or directory - pdf2htmlEX --zoom 1.3

does anyone knows how I fix it? Thanks!

Ronan Lopes
  • 3,320
  • 4
  • 25
  • 51

1 Answers1

3

The arguments need to be separate from the command. Try

Open3.popen3("pdf2htmlEX", "--zoom", "1.3", "~/test.pdf")...

eugen
  • 8,916
  • 11
  • 57
  • 65
  • But now I got the error of unrecognized option "--zoom 1.3". Removed it, and then, "I/O Error: Couldn't open file '~/test.pdf': No such file or directory.", even the file existing (and it worked if I passed the entire command as a unique string). Any ideas? – Ronan Lopes Oct 04 '16 at 14:58
  • with the absolute path, worked... but still getting error om zoom param – Ronan Lopes Oct 04 '16 at 15:01
  • all the arguments need to be specified separately in the array - I corrected the command syntax – eugen Oct 04 '16 at 15:48
  • The shell replaces '~' with your home directory. That doesn't work in a script/program. – Steffen Roller Sep 15 '21 at 02:26