1819941 Members
3526 Online
109607 Solutions
New Discussion юеВ

CSH Scripting

 
SOLVED
Go to solution
Ron Bromwell
Frequent Advisor

CSH Scripting

Can anyone tell me how to define an array of items in a csh script and how to access them.

This is what I tried:
ITEM[1]=YES
ITEM[2]=NO
ITEM[3]=MAYBE

@ COUNT = 1

echo "ITEM[$COUNT]

exit

the following is what is diplayed:
ITEM[1]=YES: No Match.

Thanks,
Ron
life's a journey, not a destination
13 REPLIES 13
Kofi ARTHIABAH
Honored Contributor

Re: CSH Scripting

You have to define ITEM itself before you define ITEM[1] ITEM[2] etc.

try

set ITEM=PLACEHOLDER
set ITEM[1]=YES
set ITEM[2]=NO
set ITEM[3]=MAYBE

set COUNT=1

echo "$ITEM[$COUNT]"

good luck

nothing wrong with me that a few lines of code cannot fix!
Shannon Petry
Honored Contributor

Re: CSH Scripting

You can not do what you would like in C-shell. C-shell only supports unary variables. So
set var[1]="yes"
However, try and set var[2] and see what you get. It should be something like...
"set: Subscript out of range.".
In order to use arrays, you need to use Korne or Borne shell.

Good luck!
Shannon
Microsoft. When do you want a virus today?
Bill McNAMARA_1
Honored Contributor

Re: CSH Scripting

Your answer is below:
I want 10 points!

% make love
Make: Don't know how to make love. Stop.

% got a light?
No match.

% sleep with me
bad character

% man: Why did you get a divorce?
man:: Too many arguments.

% rm God
rm: God nonexistent

% make 'heads or tails of all this'
Make: Don't know how to make heads or tails of all this. Stop.

% make sense
Make: Don't know how to make sense. Stop.

% make mistake
Make: Don't know how to make mistake. Stop.

% make bottle.open
Make: Don't know how to make bottle.open. Stop.

% \(-
(-: Command not found.

% rm -i God
rm: remove God? y
% ls God
God not found
% make light
Make: Don't know how to make light. Stop.

% date me
You are not superuser: date not set
Thu Aug 25 15:52:30 PDT 1988

% man rear
No manual entry for rear.

% If I had a ) for every dollar Reagan spent, what would I have?
Too many )'s.

% * How would you describe George Bush
*: Ambiguous.

% %Vice-President
%Vice-President: No such job.

% ls Meese-Ethics
Meese-Ethics not found

% "How would you rate Reagan's senility?
Unmatched ".

% [Where is Jimmy Hoffa?
Missing ].

% ^How did the^sex change operation go?
Modifier failed.

% cp /dev/null sex;chmod 000 sex
% more sex
sex: Permission denied
% mv sex show
% strip show
strip: show: Permission denied

% who is my match?
No match.

% set i="Democratic_Platform";mkdir $i;chmod 000 $i;ls $i
Democratic_Platform unreadable

% awk "Polly, the ship is sinking"
awk: syntax error near line 1
awk: bailing out near line

% %blow
%blow: No such job.

% 'thou shalt not commit adultery'
thou shalt not commit adultery: Command not found.


It works for me (tm)
Kevin Ernst
Regular Advisor
Solution

Re: CSH Scripting

Ron:

I'm no csh expert, but I think I see the problem--you need to initialize ITEM[] like this:

set ITEM=(YES NO MAYBE)

As far as I can tell, that's the only way to initialize and fill an array with values in one step. And don't forget the '$' in '$ITEM[$COUNT]' when you 'echo' the results.

I haven't even touched C shell in a long time. I'm just cheating by looking in my copy of _Unix_in_a_Nutshell_.
Tony Vilardi
Advisor

Re: CSH Scripting

#!/bin/csh -f

set count = 1
set ITEM = (yes no maybe)

while ( $count < '4')
echo "ITEM["$count"]="$ITEM[$count]
@ count = $count + 1
end
exit 0

Here is the output:
ITEM[1]=yes
ITEM[2]=no
ITEM[3]=maybe
Kevin Ernst
Regular Advisor

Re: CSH Scripting

Kofi, I tried that method before posting with both 'csh' and 'tcsh' and got 'set: Subscript out of range.' I think that doesn't work because ITEM is initialized to have exactly one element (set ITEM=PLACEHOLDER), and you can't extend it after the fact.
Kevin Ernst
Regular Advisor

Re: CSH Scripting

Shannon:

Regular *Bourne* shell doesn't support arrays (but the POSIX shell does); C shell, as far as I know, has always had arrays:

batwing 21: set ITEM=(YES NO MAYBE)
batwing 22: echo $ITEM[1]
YES
batwing 23: echo $ITEM[2]
NO
batwing 24: echo $ITEM[3]
MAYBE
batwing 25:
Ron Bromwell
Frequent Advisor

Re: CSH Scripting

Thanks all,
set ITEMS = (one two three)

worked.

Thanks again,
Ron

Anyone know of a good books on UNIX scripting?
life's a journey, not a destination
Kevin Ernst
Regular Advisor

Re: CSH Scripting

I almost hate to add yet *another* shameless (but not commerically-motivated) plug for O'Reilly (like they need any help), but the O'Reilly 'Nutshell' series are just tops. I always keep my copy of _UNIX_in_a_Nutshell_ (ISBN: 1-56592-001-5) nearby. I use the section on the Bourne/Korn shells to check the syntax on the builtins when I get rusty. Makes a handy regexp, sed, awk, and vi reference, too!

I would imagine HP's "Shells: User's Guide" (viewable at http://docs.hp.com in HTML or PDF format) would be a pretty good place to start, too.
Shannon Petry
Honored Contributor

Re: CSH Scripting

Yeah, I ooopsed! Sorry about that. I did some digging, and the array size has to be set first.
set CAR = ( 1 2 3 4 5 6 7 8 9 10 )
will give you a 10 number array.
echo $CAR
1 2 3 4 5 6 7 8 9 10
echo $#CAR
10
then you can define the indivuduals.

Sorry again for answering in haste. C-shell is a pain in the #$%^&* for scripting, so I quit using it a couple of years ago. Functions are so much nicer in Borne&Korn :)

Sincerely,
Shannon
Microsoft. When do you want a virus today?
Kevin Ernst
Regular Advisor

Re: CSH Scripting

Yeah, I actually saw the venerable Bill Hassell make a small comment along those lines in another recent post:

http://my1.itrc.hp.com/cm/QuestionAnswer/1,1150,0xbb5e7e990647d4118fee0090279cd0f9,00.html
Kevin Ernst
Regular Advisor

Re: CSH Scripting

Me, I've lately become a Bourne-shell junkie. =)
Stefan Schulz
Honored Contributor

Re: CSH Scripting

You have to initialize a variable as array first.

set x = ()

Sets this variable x as an array. You can then fill it. If you would like to use a loop or anything you can do something like:

set x = ($x $newval)

As mentioned in the previous posts you can set and fill this array in one step. Also you can always add new fields to the array. But you can't put in emty spots. Every field has to contain something.
No Mouse found. System halted. Press Mousebutton to continue.