2

我有以下命令:

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

这是一个相当基本的命令,我在其中运行带有一些命令行参数的 ruby​​ 脚本。参数是一个正-r则表达式 (#0-D)。

如果我在 Windows 上运行此命令(使用标准 Windows 命令提示符),一切都会按预期运行,但如果我尝试在 Linux 上运行相同的命令(安装相同版本的 ruby​​)。我收到以下错误:

/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

如果我从正则表达式中取出哈希/磅 (#) 符号,则命令运行正常。我做了一个测试,命令行似乎没有在 # 之后将任何内容传递到 argv 数组中。

为什么会这样,我该如何解决?

4

4 回答 4

6

查看您发布的代码上的突出显示。注意# 和它后面的所有东西是灰色的吗?这就是您在 bash 中进行评论的方式。# 之后的所有内容都被视为注释。

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

解决方案是引用评论:

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

编辑:这是一个包含更多信息的链接。

于 2011-12-05T14:27:27.720 回答
1

引用论据。#在大多数 shell 中开始注释。

$ ruby foo.rb hello there -r #wat
hello
there
-r
$ ruby foo.rb hello there -r "#wat"
hello
there
-r
#wat
于 2011-12-05T14:26:54.153 回答
1

'#' 是对某些命令行的注释。之后的一切都被忽略了。

于 2011-12-05T14:27:12.777 回答
1

#在命令行开始注释,就像在 shell 脚本中一样。

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

你必须逃避它: \#0-D

于 2011-12-05T14:27:20.280 回答