Visit Our UNIX and Linux User Community


Problem with dynamic variables outside the loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with dynamic variables outside the loop
# 1  
Old 08-21-2009
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 02:38 PM.. Reason: Adding code tags and reformat code
# 2  
Old 08-21-2009
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 https://www.unix.com/shell-programmin...s-however.html
# 3  
Old 08-21-2009
Hi Peterro,Formatted now. Can you pls have a look at it now.
# 4  
Old 08-21-2009
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  
Old 08-23-2009
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 ??

Previous Thread | Next Thread
Test Your Knowledge in Computers #229
Difficulty: Easy
According to NetMarketShare, in September 2019 Linux had a 2% global market share of the desktop / laptop computer market.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generating dynamic variables

Hi , i am unable to generate dynamic variables can any one please help me on the below issue j=1 {record_count_"$j"}=`db2 -xselect substr\(job_name,24\) rec_count from $libname.audit_table_nrt where job_name like \'DATAMART_DEL_RUN%\' and STS_FLAG=\'E\' and seq_no=$i` echo " record... (3 Replies)
Discussion started by: bhaskar v
3 Replies

2. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

3. Shell Programming and Scripting

Create, validate and using dynamic variables in Bash scripting

Hi All, I am really struggling to solve this problem, this might be small but I am not able to, can somebody help me? I have few directories and these directories receives text files in large amount with in fraction of seconds. So I just want to send all the files in current directory to... (2 Replies)
Discussion started by: VasuKukkapalli
2 Replies

4. Shell Programming and Scripting

Creating Dynamic Variables from a Flat File

Greetings all, Been trying to do my Googling and forum searches but can't seem to lock in on a solution. I have a script that parses a log and collects all the uniq events to a flat file. Some days might have 50 unique events, other days might have 75. (Hence my reference to dynamic.) ... (2 Replies)
Discussion started by: sjrupp
2 Replies

5. Shell Programming and Scripting

dynamic variables - eval - expand etc.

Hello, so i'm making a script, using dynamic variables and trying to expand them. So far it hasn't worked out too well so it seems that I need some help from you, the elite. Example: #!/bin/sh counter=0 until (($counter>5)) counter2=1 until (($counter2>6)); do if ;... (5 Replies)
Discussion started by: TehOne
5 Replies

6. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

7. Shell Programming and Scripting

Dynamic variables and eval

Hi, I need some direction with the following. The below code is semi-psuedo code which will hopefully make it easier to understand what I am trying to achieve: for i in `echo ${testarray }` do let c=c+1 eval "first$c=$i" while... (4 Replies)
Discussion started by: yonderboy
4 Replies

8. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

9. UNIX for Dummies Questions & Answers

dynamic variables

I am new to unix and the following problem is bugging me.:confused: var1="hello1" var2="hello2" var3="hello3" counter=1 while do echo $var$counter done the idea here is to display the value of "var" based on the counter. I am using the korn shell. I used array here but the... (4 Replies)
Discussion started by: max_payne1234
4 Replies

10. Shell Programming and Scripting

Dynamic variables within shell script

Hi Gurus, I have a requirement of writting the shell script where it should ask me two values FND_TOP=/d02/app/oracle/xxx/fnd/11.5.0 CDCRM_TOP=/d02/app/oracle/xxx/cdcrm/11.5.0 and then keep these values stored as variables for the execution of rest of the script. Because, I have to... (2 Replies)
Discussion started by: isingh786
2 Replies

Featured Tech Videos