Problems with variable substitution in C-Shells

Problems with variable substitution in C-Shells

Post by l.. » Sat, 01 Dec 1990 19:42:00



Hi,

I'd appreciate any answers you may have to the following simple(?) question. It
concerns variable substitution in a C-Shell:


set str = num
alias ech 'echo $\!*'

So:
(ech str) will output num, but (ech $str) won't output 5. The error message
says: variable syntax.

In other words:
$str = num, but how may I do something like $($str) ?

Thanks in advance.

Jonathan Lim

 
 
 

Problems with variable substitution in C-Shells

Post by Maarten Litmaa » Wed, 05 Dec 1990 04:33:57




)...

)set str = num
)alias ech 'echo $\!*'
)
)So:
)(ech str) will output num, but (ech $str) won't output 5. The error message
)says: variable syntax.

alias ech 'eval echo \$\!*'
--
Q: "Is there a newsgroup for astrology/para-normal?"
A: "Why don't you consult the stars and find out ??"



 
 
 

Problems with variable substitution in C-Shells

Post by Roger Rohrba » Wed, 12 Dec 1990 18:35:31



>Suppose I have:

>set str = num
>alias ech 'echo $\!*'
>So:
>(ech str) will output num, but (ech $str) won't output 5.
>In other words:
>$str = num, but how may I do something like $($str) ?

  Another job for "eval"!  The following:

    alias ech 'eval echo \$\!*'

will do what you want.  The idea here is to build the command:

    echo $num

that will echo the desired value, and then to execute it.  (This is known
as dynamic programming; Lisp programmers are big fans of this kind of thing).
So, we escape the first "$", and the shell will substitute "$str" for "\!*",
giving:

    eval echo \$num

and eval will strip off the protective "\", yielding the desire result.

--

- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "Two men need one money, but one money needs no man."               |

 
 
 

1. C-shell script variable substitution problem/question

  (Note cross-post and followup-to.)


|>      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."

--

2. Question about bash

3. How can I install Linux on a Machine with the AMD k6 processor

4. Variable substitution in a shell script

5. DNAT problem

6. shell variables/command substitution/expansion

7. SiS5597/8

8. Bourne shell and variable substitution of pipelines

9. Variable substitution in a C Shell (Summary)

10. Variable substitution using Korn shell

11. Variable substitution within a variable name

12. ZOMBIE C-shells: How to get rid of them?