4

I need to have script.sh, that would create files f1.txt and f2.txt with content that was sent to stdin. For example:

echo ABRACODABRA | script.sh

...should create files f1.txt and f2.txt with the content ABRACODABRA.

How can I do this?

Please provide script.sh's body!

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206

2 Answers2

8

You need tee.

$ echo 'foo' | tee f1.txt f2.txt 

or

$ echo 'foo' | tee f1.txt > f2.txt

to suppress the additional output to stdout.

I'm guessing your real problem could be how to read from input inside a script. In this case, refer to this question. That will give you something like

while read input; do
    echo $input | tee -a f1.txt > f2.txt
done
Community
  • 1
  • 1
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
1

Your script can look like this:

 #!/bin/bash
 read msg
 echo $msg > f1.txt
 echo $msg > f2.txt
 exit 0
joval
  • 466
  • 1
  • 6
  • 15