[comp.unix.questions] Help with functions and eval

farnham@bu-tyng.bu.edu (Dave Farnham) (02/22/90)

I am attempting to do the following in the Bourne shell.

1.  Perform a 'read' on a variable in a function through
    the position parameters.
2.  Loop while the variable contains a NULL string (force
    the user to input something).

The following code works, but I had to use a temporary variable.
I tried to use 'eval' in my test condition and I think the root
of my problems is a lack of understanding of what exactly 'eval'
does and how it works.  I know there are probably a hundred ways
to re-write my function, rather than "use this it's better".  I
would really appreciate a re-write with an explanation.

Thanks

Dave

------------------
#!/bin/sh

fun() {
tmp=""
while [ -z "$tmp" ]  # I tried to use eval here on "\$$1"
do
    echo "Enter $2 " # Enter "text string"
    read `echo $1`   # Read into a variable
    eval tmp="\$$1"  # tmp=$variable
done
}

fun var1 name
fun var2 age

echo $var1
echo $var2
------------------

Dave Farnham
( ...!uunet!wang!bu-tyng!farnham , farnham@bu-tyng.bu.edu )
Tel: (508) 649-9731
Fax: (508) 649-6926