batch file echo line with 0 does not write to file - batch-file

I have this in a batch file...
ECHO ActionNumber=0>> %wkdir%\some.ini
...problem is, it never gets written to the file but rather displayed on the console like this...
ActionNumber=
If I had...
ECHO ActionNumber=20>> %wkdir%\some.ini
...it get's written just fine
How can I write a line to this file that is simply "ActionNumber=0"
(without quotes, I'm just showing that it needs to be all one line with no spaces, no trailing space either)

>>%wkdir%\some.ini ECHO ActionNumber=20
Unfortunately, the space-after-digit solution echoes the space into the file, so you have trailing spaces.
(That's a gotcha with any digit immediately preceding a redirector)

The 0 before the < makes the parser thinks you are attempting to redirect stdin. Probably the simplest solution is to move the redirection as Peter Wright has done. Another option is to enclose the command in parentheses.
(ECHO ActionNumber=0)>> %wkdir%\some.ini

Use a Space after the 0
ECHO ActionNumber=0 >> %wkdir%\so
Edit: Thanks to #Christian K
0>> redirects to the Console stdin (stdout is 1 and stderr is 2)

Related

Can't output certain line to batch file

I'm using a batch file to create an RDP file using various variables to populate the contents.
Every line uses an Echo command and then outputs to a file with >>
For instance -
#echo screen mode id:i:1>> "C:\TEMP\file.RDP"
#echo use multimon:i:1>> "C:\TEMP\file.RDP"
Whilst this works for every line, one single line is giving me a problem and will not output -
#echo selectedmonitors:s:2,0>> "C:\TEMP\file.RDP"
For some reason, this line actually outputs selectedmonitors:s:2, (the 0 disappears) to the command window and outputs nothing into the .RDP file.
Whilst #echo selectedmonitors:s:2,0 works in the command window and outputs as expected, I can't output to a file.
What's going wrong here?
>> is just an abbreviation for 1>>, where 1 is an output stream.
There are ten of those streams:
0 is STDIN (Input) and not allowed for redirecting output.
1 is STDOUT("normal" output), 2 is STDERR (error output)
and 3 to 9 are not defined (but usable).
Remove the # to see what happens:
A batch file like
echo selectedmonitors:s:2,0>>file.txt
shows as executed command:
echo selectedmonitors:s:2, 0>>file.txt
which tries to redirect Stream 0 (STDIN) (which holds nothing here) to the file.
The reason your other lines are working is that , is a standard delimiter and : is not, so the comma snips the zero off the echo and adds it to the redirection while the colon doesn't.
Two possible workarounds:
>>file.txt echo selectedmonitors:s:2,0
(echo selectedmonitors:s:2,0)>>file.txt
Try the following, instead.
#(echo selectedmonitors:s:2,0)>> "C:\TEMP\file.RDP"
Without the parentheses, and since the , comma works as a delimiter in batch command lines (see cmd- comma to separate parameters Compared to space? for example) the last token in the command is parsed as 0>> "C:\TEMP\file.RDP" which literally means "append stream 0 output to the given file". Since stream 0 is the standard input stream, this is invalid and ignored, but the "0" parameter gets "eaten" in the process.

Batch not echoing single digit to file

In part of my code, I am trying to echo a single number to a file:
echo 1>test.txt
Except the command interpreter does not pick up the single digit, echoing
Echo is off.
to the file. Is there any way to work around this?
This is the easiest solution
>test.txt echo 1
Why does it solve the problem? What the problem is?
In batch files, input and output streams are numbered from 0 to 9: 0= stdin = default input stream, 1= stdout = default output stream, 2= stderr = default error stream and 3 to 9 available to the user.
The problem with your code is that the expression 1> file means redirect the stream 1 (standard output) to the file. The parser handles the digit as part of the redirection, not as data to send to the redirection.
To solve the problem it is necessary to separate the digit from the redirection operator, but something as echo 1 >test.txt will also output an aditional space, usually an undesired behaviour.
Changing how the command is written by reordering the line we can separate the digit and the redirection without any aditional element.
edited to adapt to comments
Can it be done without reordering?
Yes, you can also separate the command from the redirection operator enclosing the command in parenthesis
(echo 1)>test.txt
or you can add aditional indications to the parser, escaping the digit
echo ^1>test.txt
or use a for replaceable parameter to handle the digit, hiding it to the parser at redirection time
for %%a in (1) do echo %%a>test.txt
or, use delayed expansion, or .... virtually anything that ensures that the parser does not handle the digit as a stream number.

Unix script stalls/freezing on loop for some odd reason?

while read FILE;
do
echo "$FILE"
done
Pretty trivial code... but I have no idea what could possibly be messing it up...I've looked everywhere at this seems to be correct...
Did added quotations but no luck-
I'm trying to read every file in the directory
- tried adding " in $*;" to the end of the first line with no luck
So is there a way to iterate through all the files and pipe each one to read?
Ok and is there a way for it to iterate through ONLY files and not directories?
Well, it doesn't freeze up. It simply waits for input. That's what read FILE is supposed to do: read a line from standard input (=terminal unless a redirection is present) and store it in the FILE variable.
BTW, there's an extra semicolon you might want to remove; or did you perhaps mean to write
while read FILE; do
echo $FILE
done
If you meant to iterate over every file in a directory, use
for file in *; do
echo "<$file>"
done
If you meant to iterate over the arguments given to your script, use
for arg in "$#"; do
echo "<$arg>"
done
You should likely put echo "$FILE" instead of just echo $FILE. Remember the contents of $FILE will replace the variable and then the command is executed.
For example, if you have just:
echo $FILE
but the value of file is hello; shutdown, you could be in for a world of hurt. :)

Extra space appears using pipe and stdout in the same line in batch

Extra space character appears using pipe (|) and stdout(>) in the same line in a batch file (in Windows)
echo sampletext>outfile|echo text
- outfile consists of an extra space (#20) character in the first line
echo sampletext>outfile2
echo text
- works correct, but I need to make two operations per line.
Is there any ideas how to cut this space character?

Bat redirecting stderr to stdout has weird behaviour

I have a bat script which at one point redirects the stderr of a process to the stdout, and then writes it to a file. I used to do it like this:
process.exe 2>&1 > file.txt
However, this doesn't redirect the stderr to the file ( for reasons I can't understand ). When I modified the line to :
process.exe > file.txt 2>&1
The whole thing worked. Aren't these two equivalent?
The first example essentially does:
stderr = stdout;
stdout = "file.txt";
So, stderr is still pointing at the original stdout. Your second example does:
stdout = "file.txt";
stderr = stdout;
So, both stderr and stdout now reference file.txt. It's annoyingly subtle.
The redirection 2>&1 works at the end of the command line. It will not work as the first redirection parameter, the redirection requires a filename and the 2>&1 at the end. You're effectively trying to redirect stderr but there is no placeholder to store the stderr messages hence it failed. The shortcut to remembering this is
executable > some_file 2>&1
Hope this helps,
Best regards,
Tom.
By the way, for reasons I do not completely understand, a think like
process.exe > result.txt 2<&1
also seems to work

Resources