bash - Variable made of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash - Variable made of variable
# 8  
Old 02-06-2010
You can by adding a statement
Code:
k=1
while [ $k -le 10 ]
do
  V=zo${k}
  eval $V=$(awk -v k=$k {my_script} my_file)
  echo ${!V} # -> That should work !
  ((k++))
done

# 9  
Old 02-06-2010
Quote:
Originally Posted by jolecanard
To this point I knew it.

The problem is that I want to use it blindly:

Code:
k=1
while [ $k -le 10 ]
do
  eval zo${k}=`awk -v k=$k {my_script} my_file`
  echo $zo${k} -> That does not work
  k=`expr $k + 1`
done

How should I change the "echo" part?
Unusual to use eval here, try this:

Code:
k=1
while [ $k -le 10 ]
do
  zo[${k}]=`awk -v k=$k {my_script} my_file`
  echo ${zo[$k]}
  k=$(( $k + 1 ))
done

# 10  
Old 02-06-2010
I hope this clarifies:

Code:
#/usr/bin/ksh

k=1
while (( k <= 10  ))
do
    zo[${k}]="$(awk -v k=${k} 'BEGIN{print k}')"
    (( k += 1 ))
done
k=1
while (( k <= 10  ))
do
   echo "var zo${k}=${zo[${k}]}"
   (( k += 1 ))
done
exit 0

# k.sh
var zo1=1
var zo2=2
var zo3=3
var zo4=4
var zo5=5
var zo6=6
var zo7=7
var zo8=8
var zo9=9
var zo10=10

# 11  
Old 02-07-2010
Thanks,

Those two last solutions work just fine!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies

2. Shell Programming and Scripting

Bash variable within variable

Hello, Can I ask how to expand variable that contains another in bash? I need to loop variable within another one like this: RD1=testgrp RD2=testgroup RD3=testgroupfile RD4=tstgroup ... RD40=try2013 DEST=/home/king/finaldir for i in {1..40}; do mv ${RD${i}} ${DEST} done I do not... (8 Replies)
Discussion started by: yifangt
8 Replies

3. Shell Programming and Scripting

Variable in bash help

#aa=xxxx #zz="cc $aa" #aa=gggg #echo $zz out put is cc xxxx if I want to get cc gggg how should I do, I don't want to write zz="c $aa " after aa=gggg (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Use variable in bash

$ p="1 2 5 8" $ set -- $p $ echo $3 5 $ k=3 $ echo \$${k} $3 I want the "echo \$${k}" to get the output 5 , how to modify ? (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

6. Shell Programming and Scripting

[Bash]variable does not keep its value

Hello all, I have this shell script, but do not understand why the variables inside the if block does not keep its value outside. Is it because of the pipe ? How can i fix this problem ? Thank you for helping. local alarm="" local num_alarm=0 local -a alarms ... (3 Replies)
Discussion started by: trickstar
3 Replies

7. Shell Programming and Scripting

Using the value of one variable to name another variable (in bash)

Hello all, I'm working on a script, and as part of it, I'm trying to create a loop that will run through a stored piece of information a certain number of times pulling out information, and each time create a variable with a unique name that will store that information. I'm sure it's a simple... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

8. Shell Programming and Scripting

BASH get variable according to name of another variable

Just started BASH scripting, and I tried to make a script 'args' to display all of the arguments that I give to it. #!/bin/bash if then echo "No arguments specified." fi val= for ((i=1; i <= $# ; i++)) do eval "\$val=\$$i" echo "Argument number $i is $var." done However... (3 Replies)
Discussion started by: test_test
3 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question