2

I have the following command:

ruby SaveAllDatabases.rb 192.168.0.15 1024 -r #0-D --non-interactive

It's a fairly basic command in which I run a ruby script with some command line arguments. The -r argument is a regular expression (#0-D).

If I run this command on Windows (using the standard Windows command prompt), everything runs as expected, but if I try and run the same command on Linux (with the same version of ruby installed). I get the following error:

/usr/lib/ruby/1.8/optparse.rb:451:in `parse': missing argument: -r (OptionParser::MissingArgument)
  from /usr/lib/ruby/1.8/optparse.rb:1295:in `parse_in_order'
  from /usr/lib/ruby/1.8/optparse.rb:1254:in `catch'
  from /usr/lib/ruby/1.8/optparse.rb:1254:in `parse_in_order'
  from /usr/lib/ruby/1.8/optparse.rb:1248:in `order!'
  from /usr/lib/ruby/1.8/optparse.rb:1339:in `permute!'
  from /usr/lib/ruby/1.8/optparse.rb:1360:in `parse!'
  from SaveAllDatabases.rb:256

If I take the hash/pound (#) symbol out of the regex, the command runs fine. I've done a test, and the command line doesn't seem pass anything after the # into the argv array.

Why is this, and how can I work round it?

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65

4 Answers4

6

Take a look at the highlighting on your posted code. Notice how the # and everything after it are in gray? This is how you do comments in bash. Everything after the # is considered a comment.

echo "This will show on the screen" # This is a comment, it's ignored 

The solution is to quote the comment:

ruby SaveAllDatabases.rb 192.168.0.15 1024 -r '#0-D' --non-interactive

EDIT: Here's a link with more information.

Brigand
  • 84,529
  • 20
  • 165
  • 173
1

Quote the argument. # starts a comment in most shells.

$ ruby foo.rb hello there -r #wat
hello
there
-r
$ ruby foo.rb hello there -r "#wat"
hello
there
-r
#wat
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

'#' is a comment on certain command-lines. everything after that is ignored.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
1

# on the command line starts a comment, just as it would in a shell script.

marc@panic:~$ whoami #--help
marc
marc@panic:~$ whoami --help
Usage: whoami [OPTION]...

You'll have to escape it: \#0-D

Marc B
  • 356,200
  • 43
  • 426
  • 500