Click to See Complete Forum and Search --> : [Bash] Program not recognizing variables from file correctly


hop-frog
12-23-2003, 04:43 AM
Here is, much simplified, my situation:

file1 contains:#!/bin/bash
NAME="Dave"
GREETING="`tail +1 file2 | head -1`"
echo $GREETING file2 is just a data file containing the sentence structure for the greeting:Good morning, $NAME.When I run ./file1 it should be printing, "Good morning, Dave."

Instead this prints out, "Good morning, $NAME." (dollar sign included) How do I get it to replace $NAME with Dave when read from a file?

bubblenut
12-23-2003, 05:49 AM
Try

echo "Good morning,"${NAME}"."

HTH
Bubble

bwkaz
12-23-2003, 11:23 AM
Originally posted by hop-frog
GREETING="`tail +1 file2 | head -1`" I think you need an extra eval in there somewhere.

Maybe:

GREETING=$(eval echo "$(head -n 1 file2)") (BTW: the +1 and -1 options to head and tail will cause problems if you ever use a glibc newer than 2.3.2 without patching either it or coreutils (the package that head and tail come from). Those options are not POSIX compliant, and glibc >2.3.2 exports a symbol named _POSIX_VER or something like that, which causes head and tail to stop recognizing those options. The fix is to change your script to use the -n option to head and tail, and (since tail +1 prints the entire file anyway) get rid of the tail entirely. ;))

hop-frog
12-23-2003, 02:33 PM
Thanks!