首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows批处理的heredoc?

Windows批处理的heredoc?
EN

Stack Overflow用户
提问于 2009-06-19 05:01:31
回答 19查看 22.2K关注 0票数 40

有没有一种方法可以像unix shell中的heredoc那样批量指定多行字符串?类似于:

代码语言:javascript
复制
cat <<EOF > out.txt
bla
bla
..
EOF

其思想是从模板文件创建自定义文件。

EN

回答 19

Stack Overflow用户

回答已采纳

发布于 2009-06-18 22:10:28

据我所知没有。

据我所知最接近的是

代码语言:javascript
复制
> out.txt (
    @echo.bla
    @echo.bla
    ...
)

(@禁止命令echo.本身打印它正在运行的命令,并且shell允许您以空格开始一行。)

票数 34
EN

Stack Overflow用户

发布于 2013-02-23 04:14:43

这是另一种方法。

代码语言:javascript
复制
@echo off

:: ######################################################
:: ## Heredoc syntax:                                  ##
:: ## call :heredoc uniqueIDX [>outfile] && goto label ##
:: ## contents                                         ##
:: ## contents                                         ##
:: ## contents                                         ##
:: ## etc.                                             ##
:: ## :label                                           ##
:: ##                                                  ##
:: ## Notes:                                           ##
:: ## Variables to be evaluated within the heredoc     ##
:: ## should be called in the delayed expansion style  ##
:: ## (!var! rather than %var%, for instance).         ##
:: ##                                                  ##
:: ## Literal exclamation marks (!) and carats (^)     ##
:: ## must be escaped with a carat (^).                ##
:: ######################################################



:--------------------------------------------
: calling heredoc with results sent to stdout
:--------------------------------------------

call :heredoc stickman && goto next1

\o/
 | This is the "stickman" heredoc, echoed to stdout.
/ \
:next1



:-----------------------------------------------------------------
: calling heredoc containing vars with results sent to a text file
:-----------------------------------------------------------------

set bodyText=Hello world!
set lipsum=Lorem ipsum dolor sit amet, consectetur adipiscing elit.

call :heredoc html >out.txt && goto next2
<html lang="en">
    <body>
        <h3>!bodyText!</h3>
        <p>!lipsum!</p>
    </body>
</html>

Thus endeth the heredoc.  :)
:next2



echo;
echo Does the redirect to a file work?  Press any key to type out.txt and find out.
echo;

pause>NUL
type out.txt
del out.txt

:: End of main script
goto :EOF

:: ########################################
:: ## Here's the heredoc processing code ##
:: ########################################
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

示例输出:

代码语言:javascript
复制
C:\Users\oithelp\Desktop>heredoc

\o/
 | This is the "stickman" heredoc, echoed to stdout.
/ \

Does the redirect to a file work?  Press any key to type out.txt and find out.

<html lang="en">
    <body>
        <h3>Hello world!</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </body>
</html>

Thus endeth the heredoc.  :)
票数 32
EN

Stack Overflow用户

发布于 2009-06-19 06:12:24

是的,很有可能。^是文字转义字符,只需将其放在换行符之前即可。在本例中,我还添加了额外的换行符,以便在文件中正确地打印出来:

代码语言:javascript
复制
@echo off
echo foo ^

this is ^

a multiline ^

echo > out.txt

输出:

代码语言:javascript
复制
E:\>type out.txt
foo
 this is
 a multiline
 echo

E:\>
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1015163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档