如何在批处理文件或命令提示符中使用“<<”?

DMa*_*ter 13 command-line batch-file cmd.exe

我知道>>用于什么,它将所有消息写入文件而不是屏幕。我猜想<<反过来,我尝试并收到一条消息:<< was unexpected at this time.

请告诉我<<用于什么以及如何使用。

War*_*ung 16

标准的Windows命令外壳- cmd.exe-不使用<<运营商在所有

单个<意味着“将文件读入标准输入” to cmd.exe,但两个<背靠背的字符对 毫无意义cmd.exe,因此它给出了您得到的错误。

<<运算符对所有主要类型的Unix 命令 shell都有意义,它用于here-documents

$ some-command <<END
blah blah blah
blah blah
blah blah blah blah blah
END
Run Code Online (Sandbox Code Playgroud)

这三行被发送到some-command其标准输入。

这对于将大量文本发送到命令中非常有用,而无需先将其写入文件,就像您必须与<操作员一样。我经常使用它将“使用”消息嵌入到脚本中:

#!/bin/sh
if [ -z "$1" ]
then
    cat <<USAGE
usage: myscript <files...>

     Grobbles the foobie for all files given on the command line.

USAGE

    exit 1
fi

# ... do something with command line arguments
Run Code Online (Sandbox Code Playgroud)

这比编写一堆echo语句要好,因为heredoc 文本的格式与它打印到屏幕上的完全一样。此外,在这种情况下更容易处理空白、引用、重定向和变量插值。例如,请注意,我在使用消息中使用了尖括号,而无需采取任何巧妙的措施来防止 shell 尝试将它们用于 I/O 重定向。

如果你想在 Windows 上做这样的事情,你可以安装Cygwin并使用它的一个 shell。如果您使用的是 Windows 10,则可以改用WSL


脚注:

  1. 该链接进入存档的 Windows XP 文档树。微软在归档这些文档时破坏了我之前使用的链接,所以如果他们再次破坏它,这里有一个备份的第三方参考。

    cmd.exe我在 microsoft.com 上知道的唯一其他参考资料是Windows 命令 PDF(4.9 MB,948 页),它只是为大多数 (!) 内置和 Microsoft 提供的外部命令提供参考你可以在cmd提示下给。此 PDF 在两个方面是不完整的。首先,也是最相关的,这里没有对重定向如何在cmd.exeshell 中工作的综合讨论;甚至没有讨论 shell 语法。其次,PDF 的命令列表不完整:我碰巧检查的第一件事没有涵盖:diskpart.

    我相信所有这一切都源于微软明确尝试cmd.exePowerShell替换,这种尝试已经进行了很多年。在撰写本文时的最新 Windows 10 更新中,他们采取了进一步措施来隐藏 的存在cmd.exe,尽管它还没有完全消失。

    值得注意的是,PowerShell 也不支持<<重定向操作符。也不——从两个 Unix shell 的可悲回归中cmd.exe——它不支持<重定向!

  2. 开始 here-document 的规范方式是我在上面写的,在<<和分隔符之间没有空格。我模糊的回忆是,我在 shell 脚本中看到的所有 here-document 的使用也是通过这种方式完成的。here-documents 的 POSIX 规范也在其示例中使用了这种风格。

    但是,仔细阅读POSIX.1-2008 规范的其他部分会发现在<<和分隔符之间放置一定数量的空格或制表符是合法的。具体地,请参阅令牌识别规则7和10,定义io_here所述壳语法,和的定义的“空白”字符类

    就是您记录 shell 的方式。记笔记,微软。;)

    在 Bash 4 上进行测试并ksh93确认这按预期工作。

  • @BiTinerary:鉴于每天有大量人专门使用 Cygwin 以便能够在 Windows 上执行类似 Unix 的操作,我想我会在答案中留下这个提示。 (8认同)

She*_*vek 8

>>>但只有<,没有<<

command < filename        Type a text file and pass the text to command
Run Code Online (Sandbox Code Playgroud)

来源:http : //ss64.com/nt/syntax-redirection.html


小智 5

>写入新文件。

>>附加到文件

<从文件中读取

|将命令输出发送到另一个命令的输入

请参阅此处查看列表在 cmd.exe 中键入 %^ 是 Windows 复活节彩蛋吗?

自发布以来,已添加此内容。

Starting a Program
===============

See start /? and call /? for help on all three ways.

Specify a program name
--------------------------------

    c:\windows\notepad.exe

In a batch file the batch will wait for the program to exit. When
typed the command prompt does not wait for graphical
programs to exit.

If the program is a batch file control is transferred and the rest of the calling batch file is not executed.

Use Start command
--------------------------

    start "" c:\windows\notepad.exe

Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.

Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try 

    start shell:cache

Use Call command
-------------------------

Call is used to start batch files and wait for them to exit and continue the current batch file.
Run Code Online (Sandbox Code Playgroud)