有没有一种方法可以像unix shell中的heredoc那样批量指定多行字符串?类似于:
cat <<EOF > out.txt
bla
bla
..
EOF其思想是从模板文件创建自定义文件。
发布于 2009-06-18 22:10:28
据我所知没有。
据我所知最接近的是
> out.txt (
@echo.bla
@echo.bla
...
)(@禁止命令echo.本身打印它正在运行的命令,并且shell允许您以空格开始一行。)
发布于 2013-02-23 04:14:43
这是另一种方法。
@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示例输出:
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. :)发布于 2009-06-19 06:12:24
是的,很有可能。^是文字转义字符,只需将其放在换行符之前即可。在本例中,我还添加了额外的换行符,以便在文件中正确地打印出来:
@echo off
echo foo ^
this is ^
a multiline ^
echo > out.txt输出:
E:\>type out.txt
foo
this is
a multiline
echo
E:\>https://stackoverflow.com/questions/1015163
复制相似问题