using eval with set, wc, and expr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using eval with set, wc, and expr
# 1  
Old 01-07-2006
using eval with set, wc, and expr

Okay this is a mess, I'm trying to assign variables with variables in a for-loop. Here is what i have for code. The syntax is not good.

Given the following script:

#! /bin/csh

foreach site (ABC DEF GHI)

eval set \t$${site}sf = ``wc -l \$${site}.sf | awk '{print $1}'``
eval set \$${site}sf = ``expr t\$${site}sf - 1``

eval echo $site \$${site}sf

end

exit 0

I'm using eval because i'm assigning a variable with a variable. Kind of a mess, but what i'm doing is counting the lines in the file "file.sf" assigning that to the variable "t$sitesf", using expr to subtact 1, then assigning that variable to $sitesf. This works if i don't use a for-loop and just type out each one seperately, but i have like 30 different sites, so a for loop is desirable. In short, i want the number of lines, minus one, from the file $site.sf. I'm getting the error:

ABCsf: Undefined variable.
ABCsf: Undefined variable.

Any Suggestions??
# 2  
Old 01-08-2006
Decompose the statement. Keep stuff simple with eval. I don't know csh very well, so I going to just guess at the syntax. Is ABC.sf the name of a file? Or is ABC.sf the name of a variable containing the actual filename? I cannot tell which it is from your post. So I'm guessing ABC.sf is the name of the file.

foreach site (ABC DEF GHI)
set filename=${site}.sf

No eval is needed in this case! Now if you do "wc -l < $filename" instead of "wc -l $filename", wc will not output a filename and you will just get a number. And csh can do arithmetic without using expr. This is much faster. Trying this out, I seem to need spaces as I show below...

set count=`wc -l $filename`
@ count = $count - 1
eval set \$t${site}sf=$count
# 3  
Old 01-08-2006
Quote:
Originally Posted by Perderabo
Decompose the statement. Keep stuff simple with eval. I don't know csh very well, so I going to just guess at the syntax. Is ABC.sf the name of a file? Or is ABC.sf the name of a variable containing the actual filename? I cannot tell which it is from your post. So I'm guessing ABC.sf is the name of the file.

foreach site (ABC DEF GHI)
set filename=${site}.sf

No eval is needed in this case! Now if you do "wc -l < $filename" instead of "wc -l $filename", wc will not output a filename and you will just get a number. And csh can do arithmetic without using expr. This is much faster. Trying this out, I seem to need spaces as I show below...

set count=`wc -l $filename`
@ count = $count - 1
eval set \$t${site}sf=$count

Thanks your suggestion worked great. I just had to add an awk command for wc -l to get the first field, but it worked great in the end. My script went from 1025 lines to 452, much better. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Eval set -- and more

Hello everyone, I am taking a course on Lynda and they show this code below. I didn't fully understand some parts. Please see the questions within the code. Thank you for your input and time. Regards, function usage { echo Options are -r -h -b -x --branch --version --help --exclude... (9 Replies)
Discussion started by: mohca2020
9 Replies

2. Shell Programming and Scripting

Getopt eval set parameters not happening when the script is called through an application!

Hi, There is an Informatica tool through which unix scripts can be called. Now the requirement in my project is that a parent script calls a child script but this parent script has to be called through the Informatica tool. In Parent script I'm using TEMP=`getopt -o x:y: -l area:,volume:... (1 Reply)
Discussion started by: Panna
1 Replies

3. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 Replies

4. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

5. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

6. Shell Programming and Scripting

Help on eval please

Hello All, Since my variables are nested I use eval to populate the data. I have an ambiguity here when eval is used along with & say I have the below variable url="www.unix.com" , this come from function call as argument. I want to take this into another variable say... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

7. Shell Programming and Scripting

Error with expr - "expr: syntax error"

Hi All, I'm writing a shell script in KSH, where I want to store the filename, total record count and actual record count of all the source files. The source files reside in 4 different sub-folders under the same root folder. Below is code: #!/usr/bin/ksh... (6 Replies)
Discussion started by: jagari
6 Replies

8. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

9. Shell Programming and Scripting

test expr VS [ expr ]

What is the difference between test expr VS . For example : if test 5 -eq 6 echo "Wrong" and if echo "Wrong" bot will give the same output as Wrong. Now, what is the difference between these two? though they are producing the same result why we need two? Any answer will be... (2 Replies)
Discussion started by: ashok.g
2 Replies

10. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies
Login or Register to Ask a Question