简体   繁体   中英

How to wrap piped input to stdout in a bash script?

I want to write a bash script that will wrap piped input with some text.

Based on Googling and trying to pick from examples. Here is what I have so far, that does not work:

#!/bin/sh

if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
    echo "{ "template":{"name":"contact sheet template","root":"root","parameters": ["pages"]},"pages":"
    cat
    echo "}"
fi

I am receiving a JSON list from another program as piped input and I want to output before and afterwards with the above text before I pipe the results to the next program.

program_1 | wrapper.sh | program_2 > outputfile

But it doesn't output anything.

Can someone with more bash expertise point me in the right direction?

Personally I'll search in this way :

myscript.sh

echo 'BEFORE' $(cat) 'AFTER'

Do you mean your script is reading standard input from a pipe, such as

$ other-process | my-script

?

Then the commands in your script will simply inherit standard input from the pipe

#!/bin/sh

# Output preamble
cat <<EOF
{ "template":{"name":"contact sheet template","root":"root","parameters": ["pages"]},"pages":
EOF

cat   # This reads from standard input inherited from your script

# Output the closing
cat <<EOF
}
EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM