0

What I'm trying to do is pass an argument for an option in a ruby script that will be a unix command. The command may (probably will) involve greps, pipes and possible lots of other stuff. Essentially, what I'm wondering is, can a GetOptLong option be setup to accept any character as an argument. For what it's worth, I can't use OptionParser, and probably not slob either (or whatever it's called).

Thanks, -Rob

RTF
  • 6,214
  • 12
  • 64
  • 132

1 Answers1

1

Pretty sure you can just pass in your unix commands as a string and execute them from within your script.. so something like:

#getoptlong.rb

require 'getoptlong'

opts = GetoptLong.new(
  [ '--unix', GetoptLong::OPTIONAL_ARGUMENT ]
)

opts.each do |opt, arg|
  case opt
    when '--unix'
      puts `#{arg}`
  end
end

and execute the script with something like:

ruby getOptLong.rb --unix "netstat -an | grep '61613'"
  • Yes, that does work, thanks. I wasn't in a position to test it before I posted, and I probably should have waited before posting. However, if I put pipes in the command argument, the shell interprets them literally. Is there a way to escape them, backslash doesn't work (zsh) (BTW, thanks also @mu_is_too_short) – RTF Oct 21 '11 at 09:00