One-Liner Help with -e + ksh heredoc

S

s/war/peace/g;

I'm in the midst of writing a ksh script. I've got a file of junk I
need to manipulate with some hash magic in perl, but I'm trying to keep
the perl code encapsulated in my ksh script. I'm trying to do
something like this...

perl -e 'magic' -i file

But I want the magic to look more like...

perl -e <<EOF
magic
EOF
-i file

This way the code is not cryptic. So far I can not seem to make this
work... am I on crack for thinking I should be able to do something
like this? Any suggestions?
 
S

Stephane CHAZELAS

2006-10-4, 11:50(-07), s/war/peace/g;:
I'm in the midst of writing a ksh script. I've got a file of junk I
need to manipulate with some hash magic in perl, but I'm trying to keep
the perl code encapsulated in my ksh script. I'm trying to do
something like this...

perl -e 'magic' -i file

But I want the magic to look more like...

perl -e <<EOF
magic
EOF
-i file
[...]

What about

perl -e '
magic
' -i file

Or

perl -e "$(
cat << \EOF
magic
EOF
)" -i file

Or

magic=$(
cat << \EOF
magic
EOF
)

perl -e "$magic" -i file

If your system supports the /dev/fd/<n> thing:

perl /dev/fd/3 -i file 3<< \EOF
magic
EOF
 
A

anno4000

s/war/peace/g; said:
I'm in the midst of writing a ksh script. I've got a file of junk I
need to manipulate with some hash magic in perl, but I'm trying to keep
the perl code encapsulated in my ksh script. I'm trying to do
something like this...

perl -e 'magic' -i file

But I want the magic to look more like...

perl -e <<EOF
magic
EOF
-i file

This way the code is not cryptic. So far I can not seem to make this
work... am I on crack for thinking I should be able to do something
like this? Any suggestions?

You are misunderstanding ksh here documents. They don't return
a string, they supply the given text via standard input. Thus in a
ksh script

perl <<EOF
print "hihi\n";
print "haha\n";
EOF

would print the expected two lines. How well that mixes with -i
I don't know.

Anno
 
S

s/war/peace/g;

TYVM!

I had avoided using the multi line -e ' syntax because I have some
pipes and quotes in my code and other characters the shell seems to
like to interpret... hadn't thought about the cat <<EOF though that's a
neato trick. Many Thanks!

Stephane said:
2006-10-4, 11:50(-07), s/war/peace/g;:
I'm in the midst of writing a ksh script. I've got a file of junk I
need to manipulate with some hash magic in perl, but I'm trying to keep
the perl code encapsulated in my ksh script. I'm trying to do
something like this...

perl -e 'magic' -i file

But I want the magic to look more like...

perl -e <<EOF
magic
EOF
-i file
[...]

What about

perl -e '
magic
' -i file

Or

perl -e "$(
cat << \EOF
magic
EOF
)" -i file

Or

magic=$(
cat << \EOF
magic
EOF
)

perl -e "$magic" -i file

If your system supports the /dev/fd/<n> thing:

perl /dev/fd/3 -i file 3<< \EOF
magic
EOF
 
T

Tad McClellan

You are misunderstanding ksh here documents. They don't return
a string, they supply the given text via standard input.


they supply the given text right "here" (where the here-doc appears),
hence the name "here document".
 
S

Stephane CHAZELAS

2006-10-4, 12:40(-07), s/war/peace/g;:
TYVM!

I had avoided using the multi line -e ' syntax because I have some
pipes and quotes in my code and other characters the shell seems to
like to interpret... hadn't thought about the cat <<EOF though that's a
neato trick. Many Thanks!
[...]

The shell doesn't interpret anything inside '...'. So, as long
as you don't have single quotes in your perl code, it should be
OK.
 
A

anno4000

Tad McClellan said:
they supply the given text right "here" (where the here-doc appears),
hence the name "here document".

Yes, that's where the text comes from. It goes to stdin of the command
it (i.e. the leading <<EOF) is part of.

Anno
 
T

Tad McClellan

Yes, that's where the text comes from. It goes to stdin of the command
it (i.e. the leading <<EOF) is part of.


I can now see that _I_ am also misunderstanding ksh here documents.

Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top