DOS Batch File Command does not execute - windows

The following command line executes properly:
MagBoltz32 < input.txt > out.txt
The executable requires the input in brackets as above and outputs the text file. The following "batch.BAT" file (now including the escape characters) does NOT execute but simply hangs as if expecting more input.
MagBoltz32 ^< input.txt ^> out.txt
Executing batch.BAT simply results in a blinking cursor. Ultimately this command line will go into FOR loop which loops over several input/output files. I do have access to the source code.

That's not "input in brackets". The programm doesn't use any parameters (at least not in the shown syntax). It gets it's input from STDIN and writes to STDOUT.
<input.txt redirects the content of input.txt to STDIN, so the program is able to use it, like it were entered per keyboard.
>output.txt redirects STDOUT to the file output.txt instead of writing it to the screen.
Escaping characters is only needed, if you want the special char to be shown on the screen instead of "executing" it (simply spoken), so in your case, escaping the redirection characters makes no sense, but disables the redirection.

In some OS's you need to escape the pipes <, > and | so the syntax could be completely valid. This is particularly true for batch files (http://www.robvanderwoude.com/redirection.php) ... Have you checked to make sure all the files are in the same path that is executing the batch file? If you run the batch from C:\foo even though bath.BAT is in c:\bar, the input.txt will be expected to be in C:\foo.

Related

Pipe command in Bash

Pipe command is showing it's results properly .When i try to use it cat or > it doesn't show the output
i have try to run the command with different spaces but it didn't help
sort spiderman.txt | cat > superman.txt
sort spiderman.txt | > superman.txt
in the first above code cat is not showing it's output (the cat command is not showing contents of superman.txt ) however if i write is separately the cat command it's showing the contents
in the second command nothing happens to superman.txt
ideally it should have replaced all contents of superman.txt and replaced with sorted contents of spiderman.txt but nothing happens.
If you're trying simple output redirection you shouldn't pipe (|), just redirect (>):
sort spiderman.txt > superman.txt
If you want to show the content as well as redirect to a file - perhaps what you're looking for is tee?
sort spiderman.txt | tee superman.txt
Description:
The tee utility copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.
> superman.txt (with no command) is processed as follows:
superman.txt is opened for writing and truncated
The output redirection is removed from the current command.
Since there is nothing left, the empty command is treated as having
run and exited successfully. Nothing actually reads from the pipe
or writes to superman.txt.
cat is necessary as a command which does read from standard input and writes to standard output.
It sometimes seems a little odd to me that more shells don't provide a minimal built-in that simply copies input to output with no frills, to avoid otherwise having to fork and exec cat. ( I should say "no" rather than "more", as I'm not aware of any shell that does. zsh might, if I bothered to search through the documentation to find it.)
(Some shells will optimize away an extra fork when processing a command line; bash is not one of them, though. It forks once to create a process for the write end of the pipe, then forks again to run cat. I believe ksh would simply exec cat directly instead of unnecessarily forking, in which case a built-in cat is less necessary.)

Piping input from a file to a command in windows cmd

My understanding is that the redirection operator, <, should allow me to take text from a file and give it as input to another file as if I had written out the contents of that file. Here is what I am trying to do:
python code.py < input.txt
I expect this to act as though I had typed the contents of input.txt after python code.py, but instead it acts as if I passed no input.
If I use cat, I get the contents of the file:
> cat input.txt
['2015-1-1','2015-5-1','2015-9-1','2015-10-1','2015-12-1','2016-1-1','2016-2-1','2016-4-1','2016-5-1'] [65,50,30,45,55,39,45,30,20]
And if I just copy and paste the contents of the file, I get the correct behavior.
I know this must be a really simple misunderstanding on my part, but I can't figure it out.
It's called Redirection, not piping, but you are correct that the < operator will push the file to the command. You can see this in action by using Sort instead of echo.
sort < input.txt
This will display the text file as a list, sorted alphabetically. Echo does not work with text files, so sending a text file to Echo simply runs "Echo".
If you just want to send a file to the command window, you can use Type instead, and not use the redirector.
type input.txt

Windows redirect error not working using append

I'm having a weird problem that must be a matter of me doing something wrong.
When using windows command line functions I know it is possible to redirect error output using 2> nul but it is not working with the following command when I am trying to append output to a file which is intentionally read only.
(echo dataExample) >> C:\filename.txt 2> nul
I am still seeing the Access is denied message that I thought would be redirected to nul
Your command line redirects error output of command echo to device NUL which is useless as this command does not print something to STDERR.
The command line to use is:
(echo dataExample>>C:\filename.txt) 2>nul
dataExample is output by echo which is appended to file C:\filename.txt.
If the target text file is write protected, Windows command processor outputs an error message to STDERR. As any output to STDERR on execution of any command within the command block defined with the round brackets is redirected to device NUL, this error message is suppressed with that command line.
Note: A space between dataExample and >> is also written into the text file. So if no trailing space is wanted in the text file, don't specify a space left of >>. A space right of >> does not matter.

How to get rid of bash control characters by evaluating them?

I have an output file (namely a log from screen) containing several control characters. Inside the screen, I have programs running that use control characters to refresh certain lines (examples would be top or anything printing progress bars).
I would like to output a tail of this file using PHP. If I simply read in that file and echo its contents (either using PHP functions or through calling tail, the output is messy and much more than these last lines as it also includes things that have been overwritten. If I instead run tail in the command line, it returns just what I want because the terminal evaluates the control characters.
So my question is: Is there a way to evaluate the control characters, getting the output that a terminal would show me, in a way that I could then use elsewhere (e.g., write to a file)?
#5gon12eder's answer got rid of some control characters (thanks for that!) but it did not handle the carriage return part that was even more important to me.
I figured out that I could just delete anything from the beginning of a line to the last carriage return inside that line and simply keep everything after that, so here is my sed command accomplishing that:
sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g'
The output can then be further cleaned using #5gon12eder's answer:
cat screenlog.0 | sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g' | sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g'
Combined, this looks exactly like I wanted.
I'm not sure what you mean by “evaluating” the control characters but you could remove them easily.
Here is an example using sed but if you are already using PHP, its internal regex processing functionality seems more appropriate. The command
$ sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g' file.dat
will dump the contents of file.dat to standard output with all ANSI escape sequences removed. (And I'm pretty sure that nothing else is removed except if your file contains invalid escape sequences in which case the operation is ill-defined anyway.)
Here is a little demo:
$ echo -e "This is\033[31m a \033[umessy \033[46mstring.\033[0m" > file.dat
$ cat file.dat
# The output of the above command is not shown to protect small children
# that might be browsing this site.
$ reset # your terminal
$ sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g' file.dat
This is a messy string.
The less program has some more advanced logic built in to selectively replace some escape sequences. Read the man page for the relevant options.

Redirect output of a file to the write command

I am trying to redirect the standard outupt of a file to the write command and display the contents of the file (with color changes) in the terminal of the other user.
The contents of the file whose output is to be displayed is (filename is menu_sys.sh)
echo -e "\t\t\033[4;41m Welcome to Internal Messaging System \033[0;0m"
When i use the code $ sh menu_sys.sh | write 680613 the output is ^[[4;41m Welcome to Internal Messaging System^[[0;0m
Tried using the standard output redirection using &1> but that too did not work
But i need the output to be in the formatted condition.
If the write command allowed arbitrary control characters to be sent to the terminal of another user, that would be a flagrant security problem. In fact, it sanitizes the contents of the message to be sent so as to render control characters harmless. That's why you see the ESC control character as the two ASCII characters '^' and '[' on the other user's terminal.
As an aside: As Jonathan Leffler mentioned, "standard output of a file" doesn't make any sense. Actually what you appear to be doing is sending the standard output of a command (echo) to the write command, not the "standard output of a file". Or if you meant to send the contents of a file to the other user (using a command like write 680613 < somefile) that would be OK too.

Resources