[TriLUG] HERE document question

Kevin Hunter hunteke at earlham.edu
Sun Jul 25 21:44:59 EDT 2010


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.

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)

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)

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



More information about the TriLUG mailing list