Answered by:
How can I get data as input to a script from an application that writes to STDIN?

Question
-
I am by no one's definition a Powershell expert, or even a coding expert for that matter. I like to call myself a Google coder. I cobble together code from here and there to simplify tasks or accomplish a specific goal.
Below is a very simple shell script that is executed from within an application. It reads the data from STDIN and writes it to a file. I am trying to duplicate it, maybe even expand on it using Powershell. I've tried using $Input, [Console]::In, Read-Host and other ideas to no avail. I can't figure out how to get Powershell to read the data the executing application is writing to STDIN.
Pulling my hair out over this one...
- #!/bin/sh
- rc_ok=0
- while read token event_number event_data
- do
- echo $event_number $event_data >> /tmp/event.dat
- echo "$token $rc_ok"
- done
- echo "Terminating"
- exit 0
Friday, December 16, 2016 5:43 PM
Answers
-
So you have a program that writes its output to stdout, and you want to process these lines of output in a particular fashion using a PowerShell script (by reading stdin), and output the processed lines (say to stdout or a file). Is that correct?
If that's what you are asking, here is a possible PowerShell equivalent:
$rc_ok = 0 program.exe | ForEach-Object { $token,$event_number,$event_data = $_ -split '\W' "$event_number $event_data" | Out-File "event.dat" -Append "$token $rc_ok" } "Terminating"
Although in this simple example, I'm not sure what the purpose of the $rc_ok variable is, since it's always zero. This is just a rough example.
-- Bill Stewart [Bill_Stewart]
- Edited by Bill_Stewart Monday, December 19, 2016 7:05 PM
- Proposed as answer by Bill_Stewart Wednesday, December 21, 2016 3:55 PM
- Marked as answer by Richard MuellerMVP Wednesday, December 28, 2016 1:29 PM
Monday, December 19, 2016 6:15 PM
All replies
-
Sorry but this is not a Unix shell script forum. You will need to post elsewhere. This forum only supports Microft scripting languages.
\_(ツ)_/
Friday, December 16, 2016 6:15 PM -
Thank you for your reply. I'm not trying to write a UNIX script. I only included the UNIX script as as an example of what I am trying to accomplish in Powershell. If you would please read the full post, I am trying to get my Powershell script to do what the example UNIX script does.
I apologize if that wasn't clear in my original post.
- Edited by DaveWoodruff Friday, December 16, 2016 7:57 PM
Friday, December 16, 2016 7:49 PM -
Then you need to post a PowerShell script and not just a unix script.
\_(ツ)_/
Friday, December 16, 2016 8:26 PM -
I can't figure out how to get Powershell to read the data the executing application is writing to STDIN.
You can read the data from stdin the same way:
$event_number $event_data >> c:\tmp\event.dat
my blog: http://shserg.ru/
Saturday, December 17, 2016 7:21 AM -
I can't figure out how to get Powershell to read the data the executing application is writing to STDIN.
You can read the data from stdin the same way:
$event_number $event_data >> c:\tmp\event.dat
my blog: http://shserg.ru/
This > and this >> and all other redirectors of this type are output redirectors. > and >> are StdOut.
'some text' | program.exe
access the StdIn of the program where available.
\_(ツ)_/
- Proposed as answer by Richard MuellerMVP Sunday, December 18, 2016 7:18 PM
- Unproposed as answer by Bill_Stewart Wednesday, December 21, 2016 3:55 PM
Saturday, December 17, 2016 5:27 PM -
So you have a program that writes its output to stdout, and you want to process these lines of output in a particular fashion using a PowerShell script (by reading stdin), and output the processed lines (say to stdout or a file). Is that correct?
If that's what you are asking, here is a possible PowerShell equivalent:
$rc_ok = 0 program.exe | ForEach-Object { $token,$event_number,$event_data = $_ -split '\W' "$event_number $event_data" | Out-File "event.dat" -Append "$token $rc_ok" } "Terminating"
Although in this simple example, I'm not sure what the purpose of the $rc_ok variable is, since it's always zero. This is just a rough example.
-- Bill Stewart [Bill_Stewart]
- Edited by Bill_Stewart Monday, December 19, 2016 7:05 PM
- Proposed as answer by Bill_Stewart Wednesday, December 21, 2016 3:55 PM
- Marked as answer by Richard MuellerMVP Wednesday, December 28, 2016 1:29 PM
Monday, December 19, 2016 6:15 PM