[TriLUG] solved: HERE document question

Joseph Mack NA3T jmack at wm7d.net
Sun Aug 1 18:38:38 EDT 2010


On Tue, 27 Jul 2010, Kevin Hunter wrote:

> you can get finer control with the backslash character:
>
> cat > $filename <<EOF
>  if [ "\$count" = "0" ] ...
> EOF

ah. got it.

You posted something like this earlier in the thread and I 
didn't get it to work - the variable was inside a $() and I 
must have flubbed something. Since you didn't seem to 
understand the problem, I wondered if you weren't solving 
some other problem and it was irrelevant to my problem. So I 
wound up with the brute force method. Thinking some more, I 
went to the comp.unix.shell archives to see the same 
\$variable construct and figured out what you'd been talking 
about.

Here's a model HERE document generator

#! /bin/bash

source_partition=/dev/sda1

cat > foo.sh <<EOF

echo "source_partition \$source_partition"
echo "source_partition $source_partition"

count=\$(df | grep -c $source_partition)
if [ "\$count" = "0" ]
then
         echo "not mounted, backing up"
else
         echo "ERROR: source partiton $source_partition is 
mounted. Exiting"
fi

EOF

#-----------------


and the output


echo "source_partition $source_partition"
echo "source_partition /dev/sda1"

count=$(df | grep -c /dev/sda1)
if [ "$count" = "0" ]
then
         echo "not mounted, backing up"
else
         echo "ERROR: source partiton /dev/sda1 is mounted. Exiting"
fi
.
> I'm confused.  So you have it working the way you want now? 
> Since you pasted it, I'm assuming not. 
.
.
> And, as Shane said, I'm still not clear on exactly what 
> you wanted, but glad you got it fixed.

the reason I posted the brute force code to pastebin was 
because you said you didn't understand what I was trying to 
do.

> Heh.  I wouldn't say it bombed, so much as it's often 
> difficult to explain code concepts and thought processes. 
> Hell, if it were easy, would we need a LUG?  That's why I 
> suggested you post the script in it's entirety to a site 
> like pastebin.com for week or so

Then I looked at your code.

> Example changes to illustrate what you might have done:

http://dpaste.com/223883/

Thanks for the detailed code. I could splice it straight in.

I didn't understand what I should be warning people about, 
but it's obvious once I read your example. No I don't do 
that, but I will now.

As to the length of lines -

The code I posted to pastebin would have been quite readable 
if they hadn't mangled it with their own idea of where 
carriage returns should go.

I started out with 132 chars (the standard printer at the 
time) in the '60s. The right hand side of the page had 
documentation, instructions and diagrams. The left side of 
the page was usually uninterupted code with brief comments. 
If you needed more comments, you looked to the right. In DOS 
I had a great editor with configurable line length - I used 
2048chars. I do the same with vi - let the comments go off 
the screen to the right, where I can ignore them while 
editing code. If I need the comments I'll go get them. It's 
only recently that I've become aware that people want 80 
char lines. We're back to the era of punch cards before the 
132char printers. This is well before my time.

Line length seems like all the other formatting problems, eg 
how to align { and }. Some people care more about this than 
others. I don't care a whole lot. If someone wants 80chars, 
I give them 80chars, but I'm not going to accept that it's 
better. If I'm doing it for myself, I make the lines as long 
as needed.

If you look at

http://en.wikipedia.org/wiki/Indent_style

you'll notice that for all the heat that's gone into 
discussion of indenting style, there has not been one test 
conducted to determine if any of the indenting styles are 
any better than the others.

Thanks for your help and all the time you spent on this. 
I've learned quite a bit. It sure takes a long time to learn 
$shell.

Joe

-- 
Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) wm7d (dot) net - azimuthal equidistant map
generator at http://www.wm7d.net/azproj.shtml
Homepage http://www.austintek.com/ It's GNU/Linux!



More information about the TriLUG mailing list