Results 1 to 3 of 3
Hi,
I'd like to read some output from a console app, prepend some text and write the modified text to stdout. CTRL-C can be used to stop the operation. So ...
- 09-10-2010 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
BASH: how to read from piped stdout
Hi,
I'd like to read some output from a console app, prepend some text and write the modified text to stdout. CTRL-C can be used to stop the operation. So suppose I have a text file (e.g. a README) like this:
I want to be able to type this:Code:Here is some text
to get this:Code:cat README | ./prepend-with-foo
I've tried the following script but "cat ./README | ./prepend-with-foo" doesn't work; it writes many lines of "FOO:" but nothing else. Any ideas?Code:FOO: Here is FOO: some text
Code:#!/bin/bash while [ 1 ]; do read FROM_STDIN echo "FOO: $FROM_STDIN" done
- 09-10-2010 #2
Your script actually does output your STDIN but it's too fast that you could notice that. You have to exit the loop as soon as no line is left:
ButCode:while [ 1 ]; do read FROM_STDIN if [[ ! -n $FROM_STDIN ]]; then break fi echo "FOO: ${FROM_STDIN}" doneis a candidate for the Useless Use of Cat Award.Code:cat README | ./prepend-with-foo

Just useBut I wonder why you write your own script instead of using some tool like sed.Code:./prepend-with-foo < ./README
- 09-10-2010 #3Just Joined!
- Join Date
- Aug 2010
- Posts
- 18
Code:sed -e 's/^/FOO /g' ./README


Reply With Quote