The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM
global variables and dynamic allocation littleboyblu High Level Programming 3 04-09-2009 07:35 PM
dynamic variables max_payne1234 UNIX for Dummies Questions & Answers 4 09-05-2008 04:10 AM
Dynamic update loop query on Sybase database Alaeddin Shell Programming and Scripting 10 12-13-2007 06:26 AM
Dynamic variables within shell script isingh786 Shell Programming and Scripting 2 01-25-2007 09:44 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-21-2009
kk17 kk17 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 4
Problem with dynamic variables outside the loop

Hi All,

Shell is ksh

I've given portion of the script here to explain the problem.

It will accept 2 input parameters .


Code:
in_file1=$1 
in_file2=$2 
outbound_dir=/home/outbound 

for i in 1 2 
do 
  eval file$i=$outbound_dir/\$in_file$i 
  eval echo "filename is \$file$i" 
  eval temp_file=$outbound_dir/\$in_file$i 
  eval FILE$i_LINE_COUNT=`wc -l < $temp_file` 
  eval echo "Total lines in file$i are \$FILE$i_LINE_COUNT" 
done 

echo "file1 name outside loop is $file1" 
echo "file1 count outside loop is $FILE1_LINE_COUNT"

When i am displaying file1 and $FILE1_LINE_COUNT variables inside the loop its giving correct values.

But ouside the loop getting correct value for only file1 variable not getting for $FILE1_LINE_COUNT variable.

can you pls help me.

Last edited by Franklin52; 08-21-2009 at 01:38 PM.. Reason: Adding code tags and reformat code
  #2 (permalink)  
Old 08-21-2009
peterro peterro is offline
Registered User
  
 

Join Date: Jul 2009
Location: Gresham, OR
Posts: 154
First off, please format your post so it is readable. Not reading your post very closely, but based on the last sentence, have a look at Counting items in variables, How come this works, However
  #3 (permalink)  
Old 08-21-2009
kk17 kk17 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 4
Hi Peterro,Formatted now. Can you pls have a look at it now.
  #4 (permalink)  
Old 08-21-2009
peterro peterro is offline
Registered User
  
 

Join Date: Jul 2009
Location: Gresham, OR
Posts: 154
Better, please use the code tags for code.

In any case, this has nothing to do with the loop specifically. Replace


Code:
eval FILE$i_LINE_COUNT=`wc -l < $temp_file`

with


Code:
eval FILE${i}_LINE_COUNT=`wc -l < $temp_file`

You were assigning the word count output to $FILE since $i_LINE_COUNT was undefined. Surround the 'i' with curly braces to distinguish it from the rest of the line.
  #5 (permalink)  
Old 08-23-2009
kk17 kk17 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 4
thanks for your reply....it works fine with curly braces.

eval file$i=$outbound_dir/\$in_file$i
eval echo "filename is \$file$i"
eval temp_file=$outbound_dir/\$in_file$i

eval FILE${i}_LINE_COUNT=`wc -l < $temp_file`

Can i use $file$i instead of $temp_file in the above line ??
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0