How to output a line in the command prompt - cmd

I've seen people using a file that makes the command prompt output data to the console. Basically how can I make the command prompt output a line of code of my choosing like in Python's print() function.

Use echo thing if you want to echo thing
Be sure to add #echo off to the top of your .bat file

Related

.sh output to .txt file - what am I doing wrong?

I am running Windows 10 and am trying to save the error output of a test.sh file to a text file.
So I created the test.sh file and wrote an unkown command in it (i.e. "blablubb").
After that I open the terminal (cmd.exe), switch to the directory and type test.sh 2>> log.txt.
Another window opens with "/usr/bin/bash --login -i \test.sh" in the title bar, shows me "bash: blablubb: command not found" and then closes immediately.
I want to save that output because the bash-window just opens for a split second. Every google search brings me to websites talking about redirecting the output and that Stream2 ist STDERR and therefore I should use test.sh 2>> log.txt or something smiliar that takes care of the STDERR stream.
If I try the same with a test.sh file and the content:
#!/bin/bash
echo hi there
I get the output in the briefly open bash-window:
bash: #!/bin/bash: No such file or directory
hi there
But the log.txt file is empty.
If I only have echo hi therein the test.sh file I get bash: echo: command not found in the bash-window.
The log.txt also empty.
If I type the following directly in the terminal, the output is written in the log.txt:
echo hi > log.txt 2>&1
If I type directly in the terminal:
echdo hi > log.txt 2>&1
I get 'Der Befehl "echdo" ist entweder falsch geschrieben oder konnte nicht gefunden werden.' in the log.txt file.
So I guess the redirecting of the output works fine until I use test.sh.
I know that .sh files are something from the unix world and that the problem might lie there but I don't know why I can not redirect the output briefly shown in the bash-console to a text file.
The 2>> redirection syntax only works if the command line containing that syntax is interpreted by bash. So it won't work from the Windows command prompt, even if the program you are running happens to be written in bash. By the time bash is running, it's too late; it gets the arguments as they were interpreted by CMD or whatever your Windows command interpreter is. (In this case, I'm guessing that means the shell script will find it has a command line argument [$1] with the value "2".)
If you open up a bash window (or just type bash in the command one) and then type the test.sh 2>>log.txt command line in that shell, it will put the error message in the file as you expect.
I think you could also do it in one step by typing bash -c "test.sh 2>>log.txt" at the Windows command prompt, but I'm not sure; Windows quoting is different than *nix quoting, and that may wind up passing literal quotation marks to bash, which won't work.
Note that CMD does have the 2>> syntax as well, and if you try to run a nonexistent windows command with 2>>errlog.txt, the "is not recognized" error message goes to the file. I think the problem comes from the fact that CMD and bash disagree on what "standard error" means, so redirecting the error output from Windows doesn't catch the error output by bash. But that's just speculation; I don't have a bash-on-Windows setup handy to test.
It would help to know if you are running Windows Subsystem for Linux (Beta). Or if you are doing something else. I'm assuming this is what you are doing on windows 10.
If this is the case are you using bash to run the script?
Are you using win-bash?
If it is win-bash I'm not very familiar and would recommend Windows Subsystem for Linux (Beta) for reasons like this. win-bash, while cool, might not be compatible with redirection operators like 2>>.
You have stdout and stderr, by default (if you don't specify) the >> (or append) will only append standard output into the txt file.
If you use 2 it will append the standard error into the txt file. Example: test.sh 2>> log.txt
This could be better described at this site.
To get exactly the command for append both stdout and stderr, go to this page.
Please tell me if this doesn't answer your question. Also, it could be more valuable to attempt a search for this answer first and explain why your search found nothing or give more extensive clarification as to what the problem is. I love answering questions and helping, but creating a new forum page for what might be an easy answer may be ineffective. I've had a bunch of fun with your question. I hope that I've helped.
That's makes a lot of sense. Thanks Mark!
Taking what mark says into account I would get Windows Subsystem for Linux (Beta). There are instructions here. Then run your script from there.

Cant get output of command to command line itself?

I have a command typed in the command line for example: -
texteditor --help
This command executes and a window flashes and no output is given on the command line when i am expecting one. On further inspection i find that the command does give an output which i get through
texteditor --help > output.txt
This means that the command does yield an output. I have also included the path in the Environment Variables. How can i give the output to the cmd without reading from the file i.e. that i type
texteditor --help
and get the output in the same console itself.
You could try something like
FOR /F %X ('texteditor --help') DO ECHO %X
This should take the output from texteditor and echo it, line by line, on the console. If you're going to put it in a batch file, double the percent symbols.
EDITED AGAIN
I can only imagine the texteditor program is writing to a non-standard place. Normally, programs write normal messages to stdout (stream 1) and write error messages to stderr (stream 2). I obviously do not know what your particular program does, so to investigate it, I would do this:
texteditor --help 2> str2.txt 1> str1.txt
Thne you will have two files, str2.txt and str1.txt. Depending on what each contains afterwards, you should be able to see which stream you need to capture.
EDITED
In the light of your comment, maybe try this to output to console:
texteditor --help > con
I am not sure I understand what you mean by "How can I give the output to the cmd", but you can save the output of the command into the Windows Clipboard ready to paste into other applications like this:
texteditor --help | CLIP

execute user inputed Windows (or bash) commands from batch (or bash) file?

Ok, so I have this batch script and what I want to happen is that when you run the script it does some standard stuff like change the path and access files etc... but after it's done that it goes back to being a normal cmd prompt/terminal where I can type in commands at free will.
Can this be done (in either dos or bash)? Is there like an execute command that I can put in an internal while loop or is there a command where when the scirpt ends it can go back to the normal cmd/terminal?
Do you need a full bash prompt?
Or would something like this be enough?
#!/bin/bash
echo -n "Enter cmd: "
read COMMAND
echo ${COMMAND} | bash
Also, in a script, you can just execute bash and get a full prompt in the current environment.
In Dos / windows command prompt if you run the batch file from command line you will get the prompt back always by default. Just like running any other command in command prompt.
Also in windows when the batch file execution is complete you can just put Cmd.exe when everything has finished running I.e at the end of the batch file.
Hope this helps!
E.g
#echo off
Echo running
.
.
.
Cmd.exe
Or even at the end
Echo %command% | Cmd.exe
Never mind, I got a good solution using this code: (for windows)
set /p command=CMD:
%command%

Use parameter as intermediate input in Batch File

In a batch file, I am running exe which takes input from user. I want to hardcode value and continue process. check following example:
In Bat File(GetData.bat):
set /p UserInput = Enter a number?
%1
To call bat file:
GetData 5
but it's waiting for input instead of setting 5.
Note: It's just for example actually i am calling exe which takes input in process. I can't modify exe.
Command line parameters are not the same thing as standard input (stdin). There are some programs that can accept input both as command arugments or via stdin, but that is not the norm.
To get your batch script to work, you must ECHO the data to stdout, and then pipe the results to GetData.bat
echo 5|GetData
If you must provide multiple lines of input, then you can do something like
(
echo line1
echo line2
echo line3 etc.
) | yourProgram.exe
Or you can put the input in a text file and then pipe the contents of the file to your command.
type commands.txt | yourProgram.exe
Or you can redirect the input to your file
<commands.txt yourProgram
Note that some programs read the keyboard directly, and or flush the input buffer before prompting for input. You will not be able to pipe input into such a program.
Update - exe not using piped input
Most database command line programs that I have seen have an option to specify the username and password as command line arguments. You should read the documentation, or google your exe for command line arguments.
If your exe does not have an option to specify the values on the command line, then your only option is to use a 3rd party tool like AutoHotKey that can be scripted to pass keystrokes to a running program.
Look at this example:
echo Enter a number?
set UserInput=%1
echo %UserInput%
Then if you type "GetData 5", the output will look like this:
Enter a number?
--empty line to execute "set"
5

Send commands to other command-line programs

Is there a way, to send commands to another command-line program?
'Cause i have a special command-line program, but I can't send commands to it using syntax like program.exe something_to_do
the program executes something like this: ("here syntax" is where i want to input text to and also enter to start)
TheWhateverCommandLineProgram
Version 1.1
Give an option: "here syntax"
the program in code looks something like this:
echo TheWhateverCommandLineProgram
echo Version 1.1
Set opt=
set /p opt=Give an option:
if %opt%==command1 goto com1
if %opt%==command2 goto com2
...
Well, i guess so cause it wasnt me who made it (btw: off course its not called TheWhateverCommandLineProgram)
If you just want to give keyboard input to a commandline program you can just use echo and pipe it:
echo some text | program.exe
If you need more lines, then write them to a file and use input redirection:
echo one line > file
echo second line >> file
program.exe < file
I'm not 100% sure I understand what you're looking for. Here's two options:
You have two windows, each running a batch program. Let's say they are called myscript1.bat and myscript2.bat. You want to send a set of commands from myscript1.bat to be executed by myscript2.bat
You have a single batch script named myscript.bat, which executes a single program named program.exe. You want program.exe to execute some commands, or do some something.
Are either of these what you're looking for? Here's some idea:
Make myscript1.bat create a third file, mycommands.bat. Once myscript2.bat sees the file mycommands.bat exists, it will execute it and delete it. (Wow. Lame.)
Use Windows Scripting Host command (it's built in to Windows since Win2K) or Powershell (usually on most computers nowadays, if they have been updated). Either of these can send keystrokes to another program. Using those keystrokes, you can control the other program.
In what form does the other program take input? From the command prompt?
If the latter then I recommend Autohotkey: http://www.autohotkey.com/
You can use Autohotkey as a bridge and it will send the command as keypresses to the window of the other batch file.
You can ask for help in their forum. They are quite helpful.

Resources