When I run an a.exe file as below it is running fine:
C:\forc>a.exe 'iss mac'
6
(Output is 6)
How to provide this input from a text file?
I tried the below but no luck:
C:\forc>a.exe < input.txt
C:\forc>a.exe 'input.txt'
Kindly help.
the only time that you will be able to pass file contents into a program is when the program accepts a filename in the command line arguments or if the program is designed in a way that lets it read all the contents of the standard input stream. For example, in C#, you would treat Console.In in the same way you would treat a file input stream (read lines, chars, etc)
in conclusion
the program must directly support consuming data from standard input in order to use the < redirection. Standard Input is NOT the same as a command line argument.
Whether you can send input to a.exe depends entirely on a.exe. You will need to read the documentation for a.exe, or ask its author, to determine whether what you want to do is possible.
From Alexei Levenkov answer :
Using command redirection operators
program.exe < input.txt > output.txt
Related
I came across the below shell command:
$prog.sh < file_name.json
I know it reads from a file, but how and where does prog.sh load the file?
Every program has three open file handles at startup, one of which is standard input. Normally, the file handles are inherited from the parent process. The < operator tells the shell that, instead of passing its standard input to prog.sh, to open file_name.json instead and give that file handle to prog.sh as its standard input.
$prog.sh < file_name.json
As you rightly guess. The < is meant for redirecting the input from a file so that your script will read from the file which will be the (temporary) stdin(fd0).
it read from a file, but how and where prog.sh will load the file
It depends on how you plan to go about it. Any command in the script that expects an input from the stdin will now read from the file. The new line character in the text file (usually) stands for the ↵ in the stdin.
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
I would like to know if it's possible to get the output redirection file name as a parameter in bash?
For example :
./myscript.sh parameter1 > outputfile
Is there a way to get "outputfile" as a parameter like $2? In my script I have to do few operations in outputfile but I don't know which file I have to update... The second problem is, this script is already running and used by several tasks so I cannot change the user input...
Best regards
Redirections are not parameters to the program. When a program's output is redirected, the shell opens the file and connects file descriptor 2 to it before running the program. The program then simply writes to fd 2 (aka stdout) and it goes to the file.
On Linux and similar systems you can use /dev/stdout, which is a symbolic link to the process's stdout file.
I am trying to call a command in batch file using "call" method and whatever is the output of that command, I want to write in a file.
I went through from this link but cannot find the answer.
I am using this command
call %confPath% GetIniString %datFile% Keyname name >%newFile% >&1
but it creates a empty file always. How can i write the output of above command in the file?
Thanks in advance.
>%newFile% redirects the standard output to a file. in >&1, the 1 stands for standard output, and if no stream is specified, standard output is the default, so >&1 redirects on itself, although it was already redirected with the first command. So, this is illegal and shouldn't produce a file at all. In my tests, this just aborts with an errormessage.
The usual idiom 2>&1, OTOH, redirects stream 2, which is standard ERROR, to standard output, which ensures that both output and error messages end up in the file.
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.