2

Let me take bundle.bat file from RubyInstaller to represent an example.

@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"ruby.exe" "C:/Ruby200/bin/bundle" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
@"ruby.exe" "%~dpn0" %*

I don't understand this:

  1. What does @ mean here @"ruby.exe" and what for double quotes?

Here in the manual I found some explanation:

Explanation - the first line prevents the commands from being displayed, the @ in "@echo off" keeps that line from displaying.

And here are my two test scripts.

The first one:

@ECHO OFF
@ECHO "123"
ECHO "123"
PAUSE

The output:

C:\win>batch.bat
"123"
"123"

@ does't keep line from displaying.

The second:

I want to call gem environment command from batch file. And here I'm guided by the code from bundle.bat file. My code:

@ECHO OFF
@"gem environment"
PAUSE

The output is an error. Please notice two double quotes:

""gem environment"" is not a command or executable or package file.

But in bundle.bat the line @"ruby.exe" "%~dpn0" %* works.

Now I change my script:

@ECHO OFF
REM Call like this...
@gem environment
REM or call like this.
gem environment
PAUSE

Both work fine. The output:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.3
  - RUBY VERSION: 2.0.0 (2013-02-24 patchlevel 0) [i386-mingw32]
  - INSTALLATION DIRECTORY: C:/Ruby200/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: C:/Ruby200/bin/ruby.exe
  - EXECUTABLE DIRECTORY: C:/Ruby200/bin
  ... and so on

So my questions are:

  1. What is the meaning of @ character in Batch script?
  2. When do I have to double-quote the value after @?
4

1 回答 1

7

问题 1

Q1。批处理脚本中@字符的含义是什么?

@字符确实会阻止显示该行。它不会阻止显示该行的输出(如果有)。

例如:

echo foo

会显示这个:

echo foo
foo

但是添加@这样的:

@echo foo

你得到的只是输出:

foo

一旦你转身echo off@就没有用了,因为无论如何都没有显示任何线条。


问题2

Q2。我什么时候必须双引号 @ 之后的值?

示例代码中的双引号与@. 所有这些@都是导致该行不显示。

双引号可用于指向路径中包含空格的文件。例如,如果您尝试运行这样的程序:

C:\Program Files\foo.exe

shell 只会解析到第一个空格,因此它会查找名为 的文件或命令C:\Program,并返回错误。

将其更改为:

"C:\Program Files\foo.exe"

C:\Program Files\foo.exe如果存在,它将正确查找并运行它。

在您的情况下,如果您只是说gem environment,它可以找到命名的程序并作为参数gem.exe传入。environment但是,如果您引用它,"gem environment",它正在寻找名为 的东西gem environment.exe

于 2013-03-21T15:36:34.337 回答