如何重定向回显文本=1>脚本.conf



如果我发出以下命令,则从dos提示符

echo text=1>script.conf

在script.conf中,将写入text=而不是text=1我该如何解决这个问题?

谢谢。

>前面的数字是一种特殊语法,其中数字是C库意义上的文件描述符(1是stdout,2是stderr(。

当cmd.exe看到>时,它实际上会删除>前面的数字(如果有的话(和后面的文件名,然后将其余文本作为命令执行。因此,你实际上是在说;取echo text=的stdout结果并将其写入文件script.conf

始终有效的解决方案是对其进行重新排序,以便重定向优先:

>script.conf echo text=1

如果你不介意在文件的最后一行多留一个空间,你可以这样做:

echo text=1 >script.conf

感谢Stephan,我还为他的解决方案编写了一个宏:

@echo on
:: Define LF as a Line Feed (newline) string
set LF=^

::Above 2 blank lines are required - do not remove
::define a newline with line continuation
:: set ^"n=^^^%LF%%LF%^%LF%%LF%^^" this is an old style and more complex n definition
:: Next is the modern style n definition
(set n=^^^
%=EMPTY=%
)
set @{CreaPPosEth-Log.properties}=for %%. in (1 2) do if %%.==2 (for %%L in ("!LF!") DO ( %n%
set /a "I=1" %n%
for /f "tokens=* delims=" %%1 in ("!args: =%%~L!") do (%n%
set "par.!I!=!%%~1!" %n%
set /a "I+=1" %n%
)%n%
%= Inizio della vera macro =% %n%
^>!par.1! echo BillPayment=0%n%
^>^>!par.1! echo GTType=1%n%
%= Fine macro =% %n%
endlocal %n%
)) else setlocal EnableDelayedExpansion ^& set args=
set "conf1=.PPosEth.properties"
%@{CreaPPosEth-Log.properties}% conf1
:ESCI
exit /b 1000

最新更新