I have been Googling for this and searched Stack, but didn't find the exact thing I want to do. I really can't get my head around this yet.
So, let's say I have sh script which is only a wrapper of gzip (just as an example). On the commandline I want to pipe the output of something (say
mysqldump) to my script.
That script must then take the input and pipe that through to gzip.
Example of commandline:
mysqldump somedb | myscript > somefile.dmp.gz
Example of myscript (something I tried):
#!/usr/bin/env sh read inp command="echo \"$inp\" | gzip" eval $command
Probably a lot of things are wrong with this example. Please help? :)
Solution (for others looking for it)
Thanks to the explanations below I now understand that executing a command in a SH-script, will simply take the input piped to the script just as well as when the command was being executed directly.
So in this case:
cat file | gzip
will do exactly the same as:
#!/usr/bin/env sh gzip
cat file | ./myscript
So I would like to point out that in my case there was another problem as well: In my real-world script I tried to check whether there was an input stream by reading it like this:
read inp if [ -z "$inp" ]; then echo "No input" exit; fi
It turns out that after the
read inp, the input pipe is empty.