[comp.unix.questions] csh question

ellis@ultra.dec.com (David Ellis) (07/19/88)

In the csh, the expression $name or ${name} looks up the variable with the
given name and substitutes its value.

Is there any way to get the value of a variable whose name is the value of
another variable?

e.g. if we've done set x=a and set a=3, is there any simple csh expression
in terms of x that yields the value of a (3)?

Csh gives an error message "Variable syntax." if I try something like
	echo ${$x}
or even
	echo "`echo {\$}$x`"

----
David Ellis
Digital Equipment Corporation -- BXB1-1/D03 
85 Swanson Road, Boxboro MA 01719 -- (617) 264-5073
Usenet:  {ucbvax,allegra,decvax}!decwrl!ultra.dec.com!ellis
ARPA:    ellis%ultra.dec@decwrl.dec.com

hd@mh_co2.mh.nl (Theo Hardendood) (07/20/88)

From article <8807191241.AA18881@decwrl.dec.com>, by ellis@ultra.dec.com (David Ellis):
> Is there any way to get the value of a variable whose name is the value of
> another variable?
> e.g. if we've done set x=a and set a=3, is there any simple csh expression
> in terms of x that yields the value of a (3)?

set a=3
set x=a
set test=`eval echo \$$x`

Theo Hardendood
USENET: hd@mh.nl via European backbone (mcvax).
UUCP  : ..!mcvax!mhres!mh_co2!hd

mesard@bbn.com (Wayne Mesard) (07/21/88)

From article <8807191241.AA18881@decwrl.dec.com>, by ellis@ultra.dec.com (David Ellis):
> Is there any way to get the value of a variable whose name is the value of
> another variable?
> 
> e.g. if we've done set x=a and set a=3, is there any simple csh expression
> in terms of x that yields the value of a (3)?

How 'bout:

	eval echo \$$x

-- 
unsigned *Wayne_Mesard();        MESARD@BBN.COM           BBN, Cambridge, MA

When Martin Sheen visited me, he was smoking again after his heart
attack, and I asked why.  He said, 'It is my friend it is always there
and doesn't pass judgment.' I said, 'Your friend is going to kill you.'"

                                         - Larry King

ok@quintus.uucp (Richard A. O'Keefe) (07/21/88)

In article <8807191241.AA18881@decwrl.dec.com> ellis@ultra.dec.com (David Ellis) writes:
>Is there any way to get the value of a variable whose name is the value of
>another variable?
>e.g. if we've done set x=a and set a=3, is there any simple csh expression
>in terms of x that yields the value of a (3)?

One method is to use "eval".  E.g.
	% set x=a a=3
	% eval echo '$'$x
prints	3

Another method is to exploit the fact that the "set" command with
no arguments prints out the shell variables in alphabetic order,
one per line, in the form "<name><TAB><value>".  So you can do
	% set | sed -n -e "s/^$x<TAB>//p"
where <TAB> is to be replaced by an actual tab character.

Rather than using
	... `eval echo '$'$x` ...
it may be more efficient to use
	eval set y='$'$x
	... $y ...
as that doesn't involve forking another shell for the `` form.

The Bourne shell also has 'eval'
	$ x=a a=3
	$ eval y='$'$x
	$ eval echo '$'$x
sets y to 3 and prints 3.

greim@sbsvax.UUCP (Michael Greim) (07/25/88)

In article <8807191241.AA18881@decwrl.dec.com>, ellis@ultra.dec.com (David Ellis) writes:
< In the csh, the expression $name or ${name} looks up the variable with the
< given name and substitutes its value.
< 
< Is there any way to get the value of a variable whose name is the value of
< another variable?
< 
< e.g. if we've done set x=a and set a=3, is there any simple csh expression
< in terms of x that yields the value of a (3)?
< 
< Csh gives an error message "Variable syntax." if I try something like
< 	echo ${$x}
< or even
< 	echo "`echo {\$}$x`"
< 
< ----
< David Ellis
Yes. Here it is:
	eval echo \$$x

Works in all csh's I could get my hands on quickly.

	-mg
-- 
+------------------------------------------------------------------------------+
| UUCP:  ...!uunet!unido!sbsvax!greim   | Michael T. Greim                     |
|        or greim@sbsvax.UUCP           | Universitaet des Saarlandes          |
| CSNET: greim%sbsvax.uucp@Germany.CSnet| FB 10 - Informatik (Dept. of CS)     |
| ARPA:  greim%sbsvax.uucp@uunet.UU.NET | Bau 36, Im Stadtwald 15              |
| Phone: +49 681 302 2434               | D-6600 Saarbruecken 11, West Germany |
+------------------------------------------------------------------------------+
| # include <disclaimers/std.h>                                                |
+------------------------------------------------------------------------------+

mbennett@midas.UUCP (Mike Bennett) (07/26/88)

in article <8807191241.AA18881@decwrl.dec.com>, ellis@ultra.dec.com (David Ellis) says:
> 
> Is there any way to get the value of a variable whose name is the value of
> another variable?
> 
> e.g. if we've done set x=a and set a=3, is there any simple csh expression
> in terms of x that yields the value of a (3)?
> 
> Csh gives an error message "Variable syntax." if I try something like
> 	echo ${$x}
> or even
> 	echo "`echo {\$}$x`"
> 

Try "eval echo \$$x". Eval is a built-in csh (and sh) command which
effectively causes the command line to be scanned twice - once by the
shell when arguments are passed to eval, and then by the command passed
to eval (that's why the first $ is escaped). hope this helps.

Mike Bennett
Languages Development
Gould Computer Systems Division, Ft. Lauderdale, Florida
.....!{uunet,sun,pur-ee,brl-smoke}!gould!mbennett
I am paid to work and to think for my employer - so I do.
I am NOT paid to speak for my employer - so I don't.

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (11/25/88)

I use a Bourne shell script that I call "news" to read netnews.  I defined
a csh alias

	alias news '(checknews -q || \news)'

so that I can run it in either the foreground or the background.  It works
fine in both modes, but when it runs in the background and I get notification
it says something like

	[1]  + Stopped (tty output) (  < ^A > ^A )

Why the "^A"s and the redirection symbols? (This is on ultrix 2.2).

Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

chris@mimsy.UUCP (Chris Torek) (11/26/88)

In article <25971@teknowledge-vaxc.ARPA> mkhaw@teknowledge-vaxc.ARPA
(Mike Khaw) writes:
>	[1]  + Stopped (tty output) (  < ^A > ^A )
>
>Why the "^A"s and the redirection symbols? (This is on ultrix 2.2).

Old `csh'es have a bug in the job printing code, such that `||' trees
(and, I think, `&&' trees) produce this.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

nodak.edu>@adm.BRL.MIL (05/23/89)

Hello,

  A friend of mine was playing with our version of the csh on a (mostly)
Sys V.2 lookalike system, when the following sequence occured:

% set ab = (spi kev ish ere)
% echo $ab[1]
spi
% alias test ' foreach x (1 2 3) \
echo $ab[$x] \
end '
% test
? ?
DISCONNECTED from 04/118

Are we doing something incredibly stupid here ?  Or do we have a brain damaged
version of csh ?  We're running sx1100 2R1 on an sperry 1100/91 mainframe with
a binary only license.

Thanks & please forgive the spelling errors.

Patrick Spinler                         bitnet: pspinler%mkvax1@msus1
115 Parkway Apt 102                     Mankato State University
Mankato, Minnesota 56001

iwarner@zaphod.axion.bt.co.uk (Ivan Warner,G44 SSTF,6632,) (05/24/89)

From article <19712@adm.BRL.MIL>, by nodak.edu>@adm.BRL.MIL:
> 
>   A friend of mine was playing with our version of the csh on a (mostly)
> Sys V.2 lookalike system, when the following sequence occured:
> 
> % set ab = (spi kev ish ere)
> % echo $ab[1]
> spi
> % alias test ' foreach x (1 2 3) \
> echo $ab[$x] \
> end '
> % test
> ? ?
> DISCONNECTED from 04/118
> 
> Are we doing something incredibly stupid here ?  Or do we have a brain damaged
> version of csh ?  We're running sx1100 2R1 on an sperry 1100/91 mainframe with
> a binary only license.
> 

	If its any consolation, the Ultrix V3.0 version of csh did 
the same - it output "? ?", and then logged me out.

	Run from within a shell script, it does not log me out, but it
doesn't do what it should.


	Running the shell script:

                set ab = (spi kev ish ere)
                echo $ab[1]
                alias test ' foreach x (1 2 3) \
                echo $ab[$x] \
                end '
                alias test
                echo '--------------------------'
                sleep 5
                test

        using the command "csh -x cshtest" gives the output:

                set ab = ( spi kev ish ere )
                echo spi
                spi
                alias test  foreach x (1 2 3)
                echo $ab[$x]
                end
                alias test
                 foreach x (1 2 3)
                echo $ab[$x]
                end
                echo --------------------------
                --------------------------
                sleep 5
                foreach x ( 1 2 3 )

	i.e. the script dies at the "foreach" command, but with no error
message.

	Must be a bug in csh.

kamens@Neon.Stanford.EDU (Samuel N Kamens) (11/13/89)

Hi all!

I am trying to write a csh shell that looks like this (with names changed to
protect the innocent)


if ($1 == foo) then
 /usr/ucb/talk foo@bar
else
 /usr/ucb/talk $1
endif

When I run it, I get an error (error: end of file unexpected).

It's probably something really silly, but could someone tell me why this 
is wrong?

Thanks,

Sam Kamens		     |  "How do you know she is a witch?"
kamens@neon.stanford.edu     |  "She looks like one!!"
-- 
Sam Kamens		     |  "How do you know she is a witch?"

jik@athena.mit.edu (Jonathan I. Kamens) (11/13/89)

In article <1989Nov13.014412.24735@Neon.Stanford.EDU> kamens@Neon.Stanford.EDU
(Samuel N Kamens) writes:
>I am trying to write a csh shell that looks like this (with names changed to
>protect the innocent)
>
>...
>
>When I run it, I get an error (error: end of file unexpected).

  You need "#!/bin/csh -f" at the top.  Since you don't have that, it
is being interpreted as a bourne shell script, hence the error you
received.

>Sam Kamens		     |  "How do you know she is a witch?"
>kamens@neon.stanford.edu     |  "She looks like one!!"

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710

P.S. For those of you who are wondering, yes.

cpcahil@virtech.uucp (Conor P. Cahill) (11/13/89)

In article <1989Nov13.014412.24735@Neon.Stanford.EDU>, kamens@Neon.Stanford.EDU (Samuel N Kamens) writes:
> if ($1 == foo) then
>  /usr/ucb/talk foo@bar
> else
>  /usr/ucb/talk $1
> endif
> 
> When I run it, I get an error (error: end of file unexpected).

This works correctly under the csh, but gives the error you indicate when
run under the borne shell, so you are probably not running it under the 
csh (even though you call it from the csh prompt).

To ensure that the csh is used to process it, add the following to the
beginning of the file:

#!/bin/csh

I believe the # must be in column 1.  Note that this will not work when
the user is using the borne shell as his command interpreter on most non-BSD 
based systems.

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

tbc@large.cray.com (Tom Craig) (06/29/90)

I have a csh question. Is it possible to redirect stdout and stderr to 
different files in the c shell, (like 1>out 2>err in the bourne shell)?
I can't figure out how, and no one has been able to enlighten me. 

Please email the answer to tbc@crayamid.cray.com.

Thanks in advance,

-- 
Tom Craig               | The opinions expressed here are not those of CRI, its 
tbc@crayamid.cray.com   | subsidiaries or lackeys. Anyone who says otherwise
612-681-5797            | is itching for a fight. (Thank you, Michael Feldman) 

eli@panda.uucp (Eli Taub/100000) (07/02/90)

In article <7267@large.cray.com> tbc@large.cray.com (Tom Craig) writes:
>I have a csh question. Is it possible to redirect stdout and stderr to 
>different files in the c shell, (like 1>out 2>err in the bourne shell)?
>I can't figure out how, and no one has been able to enlighten me. 

Try this:                       (prog > out) >& err

A special case of the above is: (prog > /dev/tty) >& /dev/null
that will filter out all stderr.

                                                           _   |___      
Eli Taub                                                    |     |   \  |
(512) 838-4810                                                    |   /\/
Contractor at (AWD) IBM  | I express my apinions not IBM's.      /   |  \

wonus@virtech.uucp (Kevin C. Wonus) (07/09/90)

In article <7267@large.cray.com> tbc@large.cray.com (Tom Craig) writes:
>I have a csh question. Is it possible to redirect stdout and stderr to 
>different files in the c shell, (like 1>out 2>err in the bourne shell)?

Yes:
	(program_name > output) >& error

'>&' operator redirects stdout and stderr.  Therefore, stdout must be redirected
first and is enclosed in () for that reason.  After stdout is redirected, the
'>&' operator redirects only stderr.

Kevin C. Wonus
Virtual Technologies Inc.
Sterling, Virginia

maart@cs.vu.nl (Maarten Litmaath) (07/10/90)

In article <1990Jul09.150046.4238@virtech.uucp>,
	wonus@virtech.uucp (Kevin C. Wonus) writes:
)In article <7267@large.cray.com> tbc@large.cray.com (Tom Craig) writes:
)>I have a csh question. Is it possible to redirect stdout and stderr to 
)>different files in the c shell, (like 1>out 2>err in the bourne shell)?
)
)Yes:
)	(program_name > output) >& error
)
)'>&' operator redirects stdout and stderr.  Therefore, stdout must be
)redirected
)first and is enclosed in () for that reason.  After stdout is redirected, the
)'>&' operator redirects only stderr.

Right answer, wrong logic.
First we create a subshell whose stdout _and_ stderr have been redirected
to the file `error'.  _In_ the subshell we execute `program_name' with its
stdout redirected to the file `output'; we don't mess with its stderr, so
that'll still be connected to `error'.  The result of it all is stdout
going to `output' and _only_ stderr going to `error'.  QED
--
 "and with a sudden plop it lands on usenet.  what is it? omigosh, it must[...]
   be a new user! quick kill it before it multiplies!"      (Loren J. Miller)

morgan@chaos.cs.brandeis.edu (Dylan Kaufman) (05/02/91)

Hi,

I am trying to write a script which will be run by cron and which will
notify me if someone somewhere is logged on.  What I mean by that is
that the script will check to see whether I am logged on (if not, it
doesn't need to bother...) and then (assuming I am) check to see
whether there is someone logged onto another account on another
computer.  If there is, it should then determine what tty I am logged
onto and either cat or write to my tty... the problem is that it
doesn't work...  I get mail from root saying that my cron output is 

stty: : Not a typewriter.

The same thing happens when I try having it send me mail rather than
using cat or write.

The code I have is as follows:

#!/bin/csh
set a=`f|fgrep -c morgan`
if ($a != 0) then
	set b=`finger @eris.berkeley.edu|fgrep -c morgan`
	if ($b != 0) then
		set c=`w morgan|fgrep morgan|cut -f4 -d' '`
		cat ~/cronfile > /dev/$c
	endif
endif

Any help would be appreciated.  

Thank you in advance,
--
-<>Dylan<>-                     MA EMT-M, CA EMT-1A, BEMCo 107
Dylan Kaufman 			Major in Computer Science
morgan@chaos.cs.brandeis.edu	Brandeis University, Waltham, MA
------<< Support your local Emergency Medical Services >>-------
"Don't ask me, I'm just improvising" -RUSH

kre@cs.mu.oz.au (Robert Elz) (05/04/91)

morgan@chaos.cs.brandeis.edu (Dylan Kaufman) writes:

>stty: : Not a typewriter.

I'd guess that your .cshrc has an "stty" command in it, which will
break all kinds of things (it belongs in .login).  You could fix
that, or try

	#!/bin/csh -f

but you'd be much better off to use

	#!/bin/sh

and rewrite the script in sh.   Fix your .cshrc anyway.

kre