Home > Back-end >  How to redirect echo text=1>script.conf
How to redirect echo text=1>script.conf

Time:10-12

From a dos prompt if I lauch the following command

echo text=1>script.conf

in the script.conf will be written text= and not text=1 How can I solve this problem?

Thanks.

CodePudding user response:

A number right before > is a special syntax where the number is a file descriptor in the C-library sense (1 is stdout and 2 is stderr).

When cmd.exe sees > it will actually remove the number right before > (if any) and the filename after it and then execute the remaining text as the command. You are therefore effectively saying; take the stdout result of echo text= and write it to the file script.conf.

The solution that always works is to reorder it so that the redirection comes first:

>script.conf echo text=1

If you don't mind an extra space at the end of the line in the file you can do:

echo text=1 >script.conf

CodePudding user response:

Thanks to Stephan I written also a macro based to his solution:

@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
  • Related