Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect stdin in a script to another process

Tags:

bash

pipe

Say I have a bash script that get some input via stdin. Now in that script I want to launch another process and have that process get the same data via its stdin.


#!/bin/bash

echo STDIN | somecommand

Now the "echo STDIN" thing above is obviously bogus, the question is how to do that? I could use read to read each line from stdin, append it into a temp file, then

cat my_temp_file | somecommand

but that is somehow kludgy.

like image 451
janneb Avatar asked May 12 '14 09:05

janneb


2 Answers

When you write a bash script, the standard input is automatically inherited by any command within it that tries to read it, so, for example, if you have a script myscript.sh containing:

#!/bin/bash

echo "this is my cat"
cat
echo "I'm done catting"

And you type:

$ myscript.sh < myfile

You obtain:

this is my cat
<... contents of my file...>
I'm done catting
like image 173
jdehesa Avatar answered Sep 21 '22 13:09

jdehesa


Can tee help you?

echo 123 | (tee >( sed s/1/a/ ) >(sed s/3/c/) >/dev/null )
like image 45
choroba Avatar answered Sep 24 '22 13:09

choroba