Setting a variable using variables in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting a variable using variables in a loop
# 1  
Old 05-23-2013
Setting a variable using variables in a loop

Hi,

I am having a bit of trouble with the below code:

Code:
file=/path/to/file
for i in 03 06 07 21; do
if [ -f $file-$i ]; then
eval count$i=`grep -c word $file-$i`
fi
done

Totalcount=0
for i in 03 06 07 21; do
if [ ! -z "$count$i" ]; then
echo  $count$i variable not exist;
else Tcount=`expr $Tcount + $count$i`;
fi
done

echo $Tcount

When running it i am getting:

Code:
03 variable not exist
06 variable not exist
07 variable not exist
21 variable not exist

file-07 and file-21 exist, but file-03 and file-06 do not.
I am trying check if a file exists, and if it does i need to find how many times a word occurs in the file on separate lines (grep -c) and assign it to a variable. The file will be appended by 03,06,07 or 21. I do not know when each file will exist, but it is possible that all could exist or none, or just a few.

Once i have all the variables populated with the counts, i need to total them up. I do not know which variables would have been created so i need a method of checking if a variable exists.

I have been playing about with this for ages but cannot get it to work.

Any help is appreciated.

Thanks
# 2  
Old 05-23-2013
Does this work for you?

Code:
#! /bin/bash

count03=5
count06=3

Tcount=0
for i in 03 06 07 21
do
    wordcnt=$(eval echo \$count$i)
    if [ -z $wordcnt ]
    then
        echo count$i variable does not exist;
    else
        Tcount=`expr $Tcount + $wordcnt`;
    fi
done

echo "Total count is: $Tcount"

# 3  
Old 05-23-2013
If you want all the files that starts with file-, can't you do it this way?

Code:
for i in file-* do
if [-f $i ]; then
<change remaining script accordingly>

Just wondering....
# 4  
Old 05-23-2013
Variables do not work that way. You have to use eval to force it, and that's never a good idea.

Why not use an array?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variables and setting them

So I'm writing a script to generate pairwise scores for how similar two strings are, and while I've been able to get it to work on a single script, I've been unable to iterate it. So suppose I have a file thus 1234567890 1234567890 1234567899 first I need to assign two lines, by their... (3 Replies)
Discussion started by: viored
3 Replies

2. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

3. Shell Programming and Scripting

Setting Variables WITHIN For Loop in DOS Command Shell

I'm wondering if any of you could lend an assist with a small problem. First, I'm under the impression I need to use Delayed Environment Variable Expansion (DEVE), based on other things I've read across the web. Summary: trying to use command shell (cmd.exe) in XP sp3 (if that's relevant) to... (4 Replies)
Discussion started by: ProGrammar
4 Replies

4. UNIX for Dummies Questions & Answers

Need help with setting up environment variables

hi all, I would appreciate if some one could explain me the difference between setting up the variables as shown below HOME=${HOME:-"/home/user1"} HOME=/home/user1 (1 Reply)
Discussion started by: SSSB
1 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. UNIX for Dummies Questions & Answers

setting enviroment variables help

Hello everyone, I am currently trying to program in java in unix platform for the first time, so far it is OK as long as I use class libraries which come with java distribution. Unfortunately when I try to use external libraries I have to use -classpath option which I rather not doing all the... (1 Reply)
Discussion started by: run123
1 Replies

7. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

8. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

9. Shell Programming and Scripting

Setting variables in a function

I'm not quite sure what I'm doing wrong here. I've go several jobs which print reports. Occassionally a printer will break down and reports need to be move to another printer. Rather than hard code the printer names in our scripts I'm trying to set these programatically using our function... (1 Reply)
Discussion started by: BCarlson
1 Replies

10. UNIX for Dummies Questions & Answers

Setting up shell variables

Hi everyone, I am trying to set up the .profile for a user I have just created. In trying to set up the shell variables, I want to make the shell be korn shell (default shell i believe is Borne shell), so, this is what I did: SHELL=/usr/bin/ksh export SHELL Whenl executing the .profile,... (1 Reply)
Discussion started by: rachael
1 Replies
Login or Register to Ask a Question