[comp.unix.questions] C-shell script variable substitution problem/question

wurtz@iscnvx.uucp (J D Wurtz) (06/25/91)

I like scripts that are "forgiving", i.e., if I forget to supply an argument,
I want the script to automatically prompt me for the missing information.
I've run into a problem, however.  Given the following:

     1) the executable c-shell script named "sc":

               #!/bin/csh -f

               if ( $1 == "" ) then
                  echo 'Name of the directory> \c'
                  set dir = ($<)
               else
                  set dir = $1
               endif

               cat  $dir/testfile             # display the file

     2) I'm currently in directory "/home/love"

     3) the file named "testfile" is located in directory "/home/love/you"

     4) the variable "ilu" is set to "/home/love/you"

     5) IRIX System V Release 3.3.2.

I then type the following to execute script "sc" in four different ways:

     1) sc /home/love/you

     2) sc
        Name of the directory> /home/love/you

     3) sc $ilu

     4) sc
        Name of the directory> $ilu

Cases 1, 2, and 3 above all work very nicely, doing exactly what I want.
Case 4, however, does not work because shell variable "ilu" is not substituted
with its value (which is "/home/love/you").

Who can please tell me how to make the c-shell treat variable "ilu" the same
for case (4) as it does for case (3)?  Also, is this called "recursive
substitution"?  Many thanks for your assistance.
-- 
Jeffrey D. Wurtz, Staff Engineer            wurtz@sgi421.msd.lmsc.lockheed.com
Structures, Orgn/81-12, Bldg/157                      (408)756-1377
Lockheed Missiles and Space Company, Inc.
Sunnyvale, CA  94088-3504

jik@cats.ucsc.edu (Jonathan I. Kamens) (06/25/91)

  (Note cross-post and followup-to.)

In article <1991Jun24.230024.18513@iscnvx.uucp>, wurtz@iscnvx.uucp (J D Wurtz) writes:
|>      1) the executable c-shell script named "sc":
|> 
|>                #!/bin/csh -f
|> 
|>                if ( $1 == "" ) then

I personally would prefer

		if ($#argv < 1 ) then

here.

|>                   echo 'Name of the directory> \c'
|>                   set dir = ($<)
|>                else
|>                   set dir = $1
|>                endif
|> 
|>                cat  $dir/testfile             # display the file
|> 
|> ...
|> 
|>      4) the variable "ilu" is set to "/home/love/you"

Is the variable a shell variable (set with "set") or an environment variable
(set with "setenv")?  If the former, there's no way to do what you want to do,
because the subshell in which the shell script runs has no idea what the
contents of the variable are.

|> ...
|> 
|>      4) sc
|>         Name of the directory> $ilu
|> 
|> Cases 1, 2, and 3 above all work very nicely, doing exactly what I want.
|> Case 4, however, does not work because shell variable "ilu" is not substituted
|> with its value (which is "/home/love/you").
|> 
|> Who can please tell me how to make the c-shell treat variable "ilu" the same
|> for case (4) as it does for case (3)?  Also, is this called "recursive
|> substitution"?  Many thanks for your assistance.

After

		set dir = ($<)

you could add

		set dir = `eval echo $dir`

However, as I said, this will only work if the variable is an environment
variable.

In any case, I don't really know what I'd call what you're trying to do. 
"Repeated evaluation" seems as close as "recursive substitution."  See the man
page for csh for more information about "eval."

-- 
Jonathan Kamens					jik@CATS.UCSC.EDU