[TriLUG] HERE document question
Joseph Mack NA3T
jmack at wm7d.net
Mon Jul 26 17:41:54 EDT 2010
On Sun, 25 Jul 2010, Kevin Hunter wrote:
> At 5:16pm -0600 Sun, 25 Jul 2010, Joseph Mack NA3T wrote:
>> partition=/dev/sda1
>>
>> cat > restore.sh << EOF
>> count=`df | grep -c $partition`
>> EOF
>
>> I want the HERE document to be
>>
>> count=`df | grep -c /dev/sda1`
>>
>> so that the mount state is determined at the time the HERE document is
>> run, not at the time the HERE document is generated
>
> I may not be clear on exactly what you're getting at, but I'll hazard a
> guess.
>
> 1. Don't use backticks for subshell work.
Thanks for your help.
I didn't know about this, but then it's been a while since I
looked for anything new in bash. I did find an example of
nested backticks in case anyone needs to extend the idiom
:-)
word_count=` wc -w \`echo * | awk '{print $8}'\`
(at the bottom of
http://tldp.org/LDP/abs/html/commandsub.html)
I didn't know you could nest backticks either. I've wanted
to do it occasionally.
I'm using bash v2.05, in case that makes any difference. I
couldn't find any tutorials on v3 or v4 just in case your
examples only worked in the newer versions.
> Suggest you don't use the backticks for subshell
> evaluation. They're deprecated for a number of reasons,
> but the easiest reason is for forward compatibility and
> nestability. Consider the $( ) construct instead:
>
> count=$(some set | of subshell | work)
> count=$(some set $(of subshell) | work)
OK
count=$(df | grep -c /dev/sda1)
is the same as
count=`df | grep -c /dev/sda1)`
> 2. I think your issue is when the command gets evaluated
> and when the string gets interpreted. Specifically, you
> want the string interpreted, but you want the command
> evaluated later. Try this:
>
> command="df | grep -c $partition"
> count=\$($command)
I've split the problem into two parts.
o I have to write a HERE document where some variables are
substituted before the HERE document is written AND some
variables are not substituted till the HERE document is run.
Yesterday, when I first had a go at this, I expected to find
examples explained in the Advanced BASH scriping guide. I
thought this would be routine (if not a little tricky).
o The HERE document has to run
I'm tackling the 2nd part first, since this turns out to be
a harder problem than I thought. I'm writing the HERE
document by hand at the moment. When I get something that
works and I can generate from another script, I'll tackle
the generating script.
Here's the code inside the HERE document (/dev/sda1 was the
result of substitution in the generating script)
command="df | grep -c /dev/sda1"
I now want to execute command (I run the code with sh -x )
count=\$($command) #syntax error
count=$(command) #$(command) is blank
count=$($command) #the -c in the command is interpreted as an option to df
#using \| doesn't help
Any more ideas?
Thanks Joe
>
> The backslash says "interpret this following character literally, not as a
> special operator". So, the $command token will get interpreted into your
> restore.sh, but the \$( ... ) will get put in as $( ... ), and not an
> evaluation. Note that if you were to try count=$($command) (no backslash),
> the line would be interpreted with automatic escaping, which is probably not
> what you want.
>
> count=$(df \| grep -c $partition),
>
> HTH,
>
> Kevin
>
--
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