2

I have to run some shell commands where the user gives the input. I found one way which seemed secure: system *%W(ls #{file}) [here].

I need to get the output of that command, so I cant just use system. Is there a way to sanitize the command for backticks `` or for %x[]?

Community
  • 1
  • 1
klump
  • 3,259
  • 2
  • 23
  • 26

2 Answers2

3

You want IO::popen instead of system. You can still pass an array of strings to invoke the command without a shell, and you can read from the resulting IO object.

If you want to read stderr too, then use the open3 module instead of IO.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

What kind of shell commands are you running that Ruby cannot support? If you are listing files, use Dir

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
kurumi
  • 25,121
  • 5
  • 44
  • 52
  • i was using ls just as an exsample, i want to use a mix of selfwritten programms and unix programms, i am using the ruby File, Dir and FileUtils classes :P – klump Mar 20 '11 at 13:15