[comp.unix.shell] Piping here documents

rhl@grendel.Princeton.EDU (Robert Lupton (the Good)) (02/08/91)

What's the best way of piping a here document into a long shell script?
For a one liner
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#!/bin/sh
cat <<-EOF | sed -e s/Hello/Goodbye/
	Hello world
EOF
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
works just fine, but if the rest of the commands (here just sed) don't
fit on a line this doesn't work. The best that I could come up with was:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#!/bin/sh
if true; then
cat <<-EOF
	Hello world
EOF
fi | sed -e s/Hello/Goodbye/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Any ideas?

				Robert

pfalstad@dae.Princeton.EDU (Paul Falstad) (02/08/91)

rhl@grendel.Princeton.EDU (Robert Lupton (the Good)) wrote:
>What's the best way of piping a here document into a long shell script?
>For a one liner

>#!/bin/sh
>cat <<-EOF | sed -e s/Hello/Goodbye/
>  Hello world
>EOF

These should give you some ideas:

#!/bin/sh
cat <<EOF | cat | cat |
Hello world
EOF
sed -e s/Hello/Goodbye/

or:

#! /bin/sh
cat <<EOF | (
Hello world
EOF
sed -e s/Hello/Goodbye/
echo foo
echo bar
)

If this won't help for what you're trying to do, please clarify.

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
10 PRINT "PRINCETON CS"          | #include <std.disclaimer.h>
20 GOTO 10                       | [Your blood pressure just went up.]
Princeton University would like to apologize to everyone for this article.

]) (02/08/91)

In article <6061@idunno.Princeton.EDU> rhl@grendel.Princeton.EDU (Robert Lupton (the Good)) writes:
>What's the best way of piping a here document into a long shell script?

I usually do multi-line here-documents in a function...

--- start sample ---
:
# sh or ksh

#
# Database search function
#
searcher() {
	dbquery $DBNAME <<EOF
	retrieve ( r.a, r.b, r.c ) where
		r.a = $astring and
		r.b = $bstring
		$sort
EOF
	}

#
# Start of mainline code (usually done with getopts and the like,
# so this is just a brief alternative without arg testing etc.)
#
astring="${1:-*}"
bstring="${2:-*}"
sort="sort by ${3:-r.a}"
DBNAME="${4:-mydatabase}"

#
# call searcher to get records from the db,
# sed to delete the last line (row count),
# and lp the results.
#
# (Talk about nonsense!!  :-)
#
searcher |
sed '$d' |
lp

exit $?
--- end sample ---

...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs@uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]

dave@aspect.UUCP (Dave Corcoran) (02/13/91)

> For a one liner
> works just fine, but if the rest of the commands (here just sed) don't
> fit on a line this doesn't work. The best that I could come up with was:

#!/bin/sh
cat <<@@ | sed '
s/1/a/
s/2/b/
s/3/c/
s/4/e/
'
11
22
33
44
55
@@

-- 
David Corcoran		      -@@
uunet!aspect!dave	        ~
In a society where anything goes eventually everything will.