# C-shell (eval array) problems.



## valsorym (Jul 8, 2012)

Hi guys!
I have a problem with my C-shell program.
For example. 
We have 3 position in office: manager, accountant and administrator. Everyone positions has descriptions: person name, person age, person sex and her phone. We need create cycle and show all personal data.
P.S. The problem was invented - just an example. The purpose of the issue conversion of strings to the command (maybe using eval).


```
#!/bin/csh

#            NAME    AGE  SEX PHONE
set manager=('M.Jon' '21' 'M' '7263627')
set accountant=('J.Melisa' '30' 'W' '6473647')
set administartor=('K.Alen' '25' 'M' '7263127')

set evalvar='set current=($manager)'
eval $evalvar

printf "\
NAME:\t$current[1]\
AGE:\t$current[2]\
SEX:\t$current[3]\
PHONE:\t$current[4]\n"

exit(0)
```

It's works. We converted text "manager" to script-variable and write this data to current variable.
But, we should review all positions. For this I create array:
set office=(manager accountant administrator)
I'm trying to take the first element of the array and get his data:

```
#!/bin/csh

#            NAME    AGE  SEX PHONE
set manager=('M.Jon' '21' 'M' '7263627')
set accountant=('J.Melisa' '30' 'W' '6473647')
set administartor=('K.Alen' '25' 'M' '7263127')
set office=(manager accountant administrator)

set evalvar='set current=(\$'"${office[1]})"
printf "\nIn EVALVAR:\n$evalvar\n"  # Here I see that the variable $evalvar 
                                    # is written string 'set current=($manager)', 
                                    # the same line as the previous example.
eval $evalvar

printf "\
NAME:\t$current[1]\
AGE:\t$current[2]\
SEX:\t$current[3]\
PHONE:\t$current[4]\n"

exit(0)
```

This is not working. 
Help me guys - how to take the word of the array, convert it to a variable and get the data out of it?

P.S. Sorry for my English.


----------



## valsorym (Jul 9, 2012)

Hi guys, it's i am again.
At first I tried use source C-shell method:

```
...

set evalvar='set current=(\$'"${office[1]})"
set tmpfile="/tmp/abs_$$"
echo $evalvar >! $tmpfile
source $tmpfile

...
```

I am getting an error message  Then I opened and looked up the temporary file: $tmpfile.

```
set current=(\$manager)
```
We have screened a symbol $ !!! 
Wow, my bad set evalvar='set current=(\$'"${office[1]})", I should write: set evalvar='set current=($'"${office[1]})".
***Notice the code: ...'set current=($'... - no backslash symbol.

Okay, full result:

```
#!/bin/csh

#            NAME    AGE  SEX PHONE
set manager=('M.Jon' '21' 'M' '7263627')
set accountant=('J.Melisa' '30' 'W' '6473647')
set administartor=('K.Alen' '25' 'M' '7263127')
set office=(manager accountant administrator)

while ($#office > 0)
	set evalvar='set current=($'"${office[1]})"
	eval $evalvar

	printf "\
		NAME:\t$current[1]\
		AGE:\t$current[2]\
		SEX:\t$current[3]\
		PHONE:\t$current[4]\n"

	shift office
	unset current
end # end while ().

exit(0)
```

It is works!
Thanks all, sorry me for my carelessness :r


----------



## SirDice (Jul 9, 2012)

Although I love csh(1), don't use it for scripting.

Csh Programming Considered Harmful


----------



## valsorym (Jul 9, 2012)

SirDice said:
			
		

> Although I love csh(1), don't use it for scripting.
> 
> Csh Programming Considered Harmful



Hmm... What scripting language you are using?

I used sh(1) - it has a function (useful), but there's no arrays (that annoy).
bash(1) - a very powerful language, a lot of documentation - a lot of opportunities. But this shell - heart with Linux systems.
FreeBSD uses the default tcsh(1) - a C-shell. There is not functional, but I can use of the individual scenarios  as a function. But - here has arrays, and C like syntax - I love to C programming language.

I do not use Python or Perl because they have to install. It is better to write programs in C, compile it and run. For smaller jobs, I use the standard interpreter - e.t.sh(1) / tcsh(1).

I have a question for you:
1. Why is it harmful?
2. What kind of interpreter you use?

P.S. I read your article "Csh Programming Considered Harmful" - but it does not give an answer to the first question to you. In this paper, blogged how better bash(1) to  csh(1) - I know it. But to automate a small process - what is harmful csh(1)?

Yes, csh(1) weak scripting language - very corny. But it is not harmful, I think so.  :stud


----------



## SirDice (Jul 9, 2012)

If you look closely all scripts in FreeBSD are written using sh(1).


----------



## valsorym (Jul 9, 2012)

SirDice said:
			
		

> If you look closely all scripts in FreeBSD are written using sh(1).


Hmm. This is a good argument. :r


----------

