简体   繁体   English

在Windows中的ruby中,执行cmd提示命令“ move”会给出错误“命令的语法不正确。”

[英]in ruby in windows, executing the cmd prompt command 'move' gives error “The syntax of the command is incorrect.”

in ruby in windows, executing the cmd prompt command 'move' gives error "The syntax of the command is incorrect." 在Windows中的ruby中,执行cmd提示命令“ move”会给出错误“命令的语法不正确”。

But it works outside of ruby 但它在红宝石之外工作

C:\rubytest>echo asdf>c:\techprogs\azzz.azz

C:\rubytest>del c:\techprogs\azzz.azz

C:\rubytest>echo asdf>c:\techprogs\azzz.azz

C:\rubytest>move /y c:\techprogs\azzz.azz c:\techprogs\autorun.bat
        1 file(s) moved.

C:\rubytest>move /y c:\techprogs\azzz.azz c:\techprogs\autorun.bat
The system cannot find the file specified.

C:\rubytest>

All of that above is fine and expected. 上面所有这些都是可以预期的。

Notice I never get an error that says "The syntax of the command is incorrect." 注意,我永远不会收到一个错误消息,指出“命令的语法不正确”。

Then try in ruby 然后尝试红宝石

I have a simple file with one line 我有一个简单的文件,只有一行

C:\rubytest>type syntaxcommandincorrect.rb
`move /y c:\techprogs\azzz.azz c:\techprogs\autorun.bat`

C:\rubytest>

But it gives that error about the syntax 但这给了语法错误

C:\rubytest>del c:\techprogs\azzz.azz

C:\rubytest>ruby syntaxcommandincorrect.rb
The syntax of the command is incorrect.

C:\rubytest>echo asdf>c:\techprogs\azzz.azz

C:\rubytest>ruby syntaxcommandincorrect.rb
The syntax of the command is incorrect.

C:\rubytest>

The problem here is probably the backslashes which have significant meaning inside of interpolated Ruby strings, double-quoted but also backtick-style shell commands. 这里的问题可能是反斜杠,在插入的Ruby字符串内部,反斜杠具有重要的意义,双引号还包括反引号样式的shell命令。

As such your command is being interpreted as: 因此,您的命令被解释为:

move /y c:^Iechprogs^Gzzz.azz c:^Iechprogs^Gutorun.bat

Where ^I is "\\t" which is a tab character, and ^G is "\\a" which is a bell character . 其中^I"\\t" ,它是一个制表符,而^G"\\a" ,这是一个响铃字符

Instead: 代替:

`move /y c:\\techprogs\\azzz.azz c:\\techprogs\\autorun.bat`

Now remember that Ruby has a very comprehensive library of functions you can use to address this directly. 现在请记住,Ruby有一个非常全面的函数库,您可以使用该函数库直接解决此问题。 Don't treat it like a fancy shell scripting language: 不要将其视为精美的shell脚本语言:

require 'fileutils'

FileUtils.mv('c:\techprogs\azzz.azz', 'c:\techprogs\autorun.bat', force: true)

Where here I'm using single quotes to avoid the double backslashes and force: true is the equivalent of /y . 在这里我使用单引号来避免双反斜杠和force: true等效于/y This uses FileUtils.mv , part of a whole package of useful file and directory manipulation utilities. 它使用FileUtils.mv ,它是有用的文件和目录操作实用程序的整个软件包的一部分。

As a plus you also get proper exceptions if something goes wrong, or an error code if the move failed. 另外,如果出现问题,您还将获得适当的例外;如果移动失败,则将获得错误代码。

Added by barlop 由barlop添加

Confirming the above. 确认以上。 Double backslash fixes it, and i see via doing puts `echo copy /yc:\\techprogs...` what happens with single backslash, I see the t of techprogs removed, as c:\\techprogs became c:<ascii-9>echprogs. 双反斜杠解决了这个问题,我通过执行puts`echo copy / yc:\\ techprogs ...`看到单反斜杠会发生什么,我看到techprogs的t被删除了,因为c:\\techprogs变成c:<ascii-9>echprogs. And \\autorun became <ascii-7>utorun 并且\\autorun成为<ascii-7>utorun

C:\rubytest>cmdoutoutwithoutinitbat.rb | xxd
0000000: 6162 6364 6566 670d 0a63 6f70 7920 2f79  abcdefg..copy /y
0000010: 2063 3a09 6563 6870 726f 6773 0775 746f   c:.echprogs.uto
0000020: 7275 6e2e 6261 7420 633a 0965 6368 7072  run.bat c:.echpr
0000030: 6f67 7307 7a7a 7a2e 617a 7a0d 0a61 6263  ogs.zzz.azz..abc
0000040: 6465 6667 0d0a 6d6f 7665 202f 7920 633a  defg..move /y c:
0000050: 0965 6368 7072 6f67 7307 7a7a 7a2e 617a  .echprogs.zzz.az
0000060: 7a20 633a 0965 6368 7072 6f67 7307 7574  z c:.echprogs.ut
0000070: 6f72 756e 2e62 6174 0d0a                 orun.bat..

C:\rubytest>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM