Dynamic variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic variable assignment
# 1  
Old 11-22-2010
Dynamic variable assignment

Hi all,

I’m very new to UNIX programming. I have a question on dynamic variable

1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields.

Say number of fields equal to N

Code:
N=`cat ffvldtn_test.txt | awk 'BEGIN{FS="|"};{print NF}'`

From the above, I’m getting the fields count equal to five.

Code:
n=1
 
 
while [ $n -le $NF ];
do
 
eval "eval_var=\$var${n}"
eval_var=`cat ffvldtn_test.txt | cut -d '|' -f${n}`
  echo "var${n}=${eval_var}"
((n=n+1))
done


The above script is working as expected. But the real thing is that I can’t re call the variables when I required in the script.

Code:
echo $var1
echo $var2
echo $var3
echo $var4
echo $var5

all are giving blank…

please help out to fix this

Moderator's Comments:
Mod Comment Please use code tags. You've got an PM explaining the details

Last edited by pludi; 11-22-2010 at 09:03 AM..
# 2  
Old 11-22-2010
I guess "|" is your field separator, right?
try this:
Code:
read -a arr <<< `sed 's/|/ /g' | your_input_file_here`
for e in ${arr[@]}; do
    echo $e
done

# 3  
Old 11-22-2010
Quote:
Originally Posted by kevintse
I guess "|" is your field separator, right?
try this:
Code:
read -a arr <<< `sed 's/|/ /g' | your_input_file_here`
for e in ${arr[@]}; do
    echo $e
done


yes kevintse."|" is my field separator...thanks for ur reply...let u know if its work

---------- Post updated at 07:38 AM ---------- Previous update was at 07:32 AM ----------

kevintse,

its not at all working...better u chnage my code and post it...
# 4  
Old 11-22-2010
Why don't you use arrays?

An example how to use arrays and "dynamic variables" if you persist to use that:
Code:
set $(awk 'BEGIN{FS="|"};{print NF}' ffvldtn_test.txt)

n=1

for i in $@
do
  arr[$n]=${i}		# fill array
  eval "var_${n}=${i}"	# fill dynamic variable
  n=$(( $n + 1 ))
done

echo arr[1]
echo arr[2]
echo arr[3]
echo arr[4]
echo arr[5]

echo $var1
echo $var2
echo $var3
echo $var4
echo $var5

# 5  
Old 11-22-2010
Quote:
Originally Posted by mkarthykeyan
yes kevintse."|" is my field separator...thanks for ur reply...let u know if its work

---------- Post updated at 07:38 AM ---------- Previous update was at 07:32 AM ----------

kevintse,

its not at all working...better u chnage my code and post it...
Why, what error messages did you get?

can you run this code on your machine:
Code:
#!/bin/bash

read -a arr <<< `echo "hello|world|test" | sed 's/|/ /g'`
for e in ${arr[@]}; do
    echo $e
done
#in my test, it prints
hello
world
test

# 6  
Old 11-23-2010
$ sh test_1
test_1[2]: syntax error at line 2 : `<' unexpected
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Dynamic variable assignment

#!/bin/sh if then echo "Insufficient number of arguments ">> error".log" ; echo "Please check error log for more details"; exit 1 ; else file_name=$1".csv"; fi ; in_par_number=`head -n1 $file_name | sed 's///g' | wc -c` read -a arr <<< `head -n1 $file_name | sed 's// /g'` ... (6 Replies)
Discussion started by: JayDoshi
6 Replies

3. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

4. Shell Programming and Scripting

Dynamic variable assignment

My Code : -------------------------------------------- #!/bin/bash for i in `echo server1 server2` do eval ${i}_name = "apache" echo ${i}_name done -------------------------------------------- Current output : >./test.sh ./test.sh: line 5: server1_name: command not found... (3 Replies)
Discussion started by: sameermohite
3 Replies

5. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

6. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

7. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

8. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

9. Shell Programming and Scripting

Dynamic variable assignment

Hi All, I have the below scenario: A file test.cfg with three fields>> DATA1 DATA2 DATA3 In the script I need to assign each of the fields to variables. The number of fields will not be constant (this case we have three). Im trying to do something like this: NUM=1 OUT_DAT_NO=3 ... (4 Replies)
Discussion started by: deepakgang
4 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question