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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using the value of one variable to name another variable (in bash)
# 1  
Old 06-18-2009
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 syntax problem, but I've gone through it several different ways and I can't seem to find my mistake.

Code:
(05:15:30\[ddecosta@S.Man)
[~/bin]$ cat x
#!/usr/bin/bash

#Define Colours
red='\e[0;31m'
nc='\e[0m'

#Make certain that we have something to check
if [ -z $1 ];then
echo -e " ${red}You Must Specifiy what MOD you wish to check${nc}"
exit 1
fi

#Get QPSK info
lq=`listQpsk $1`
echo "Counting Number of DeMODS"
#Establish number of DeMods on QPSK 
num_of_mods=`echo "$lq"|awk '{print $3}'|grep -cw [1-9]`
#get current count for each DeMOD
modseq="1"
until [[ $modseq -gt $num_of_mods ]];do
count"$modseq"=`echo "$lq"|grep -w $modseq|awk '{print $5}'`
echo "$count"$modseq""
modseq=$(($modseq+1))
done

(05:15:17\[ddecosta@S.Man)
[~/bin]$ ./x 42
Counting Number of DeMODS
./x: line 40: count1=824: command not found
1
./x: line 40: count2=130: command not found
2
./x: line 40: count3=865: command not found
3
./x: line 40: count4=829: command not found
4
./x: line 40: count5=948: command not found
5
./x: line 40: count6=611: command not found
6
./x: line 40: count7=666: command not found
7
./x: line 40: count8=967: command not found
8


(05:15:36\[ddecosta@S.Man)
[~/bin]$

Just so you can see what the listQpsk value is:

Code:
(05:15:01\[ddecosta@S.Man)
[~/bin]$ listQpsk 42
 ID QPSK Name           Demod   IS2W    SignOn  % SignOn
--- ------------------  -----  -----  --------  --------
 42 SMANHUBAQPSK3          -       0         0        -
 42 SMANHUBAQPSK3          1     988       824        83
 42 SMANHUBAQPSK3          2     169       130        76
 42 SMANHUBAQPSK3          3    1014       865        85
 42 SMANHUBAQPSK3          4     971       829        85
 42 SMANHUBAQPSK3          5    1129       948        83
 42 SMANHUBAQPSK3          6     756       611        80
 42 SMANHUBAQPSK3          7     796       666        83
 42 SMANHUBAQPSK3          8    1201       967        80
--- ------------------  -----  -----  --------  --------
    Total (IS2W, signed on)     7024      5840        83
Jun 18 05:15:17 - listQpsk Ended.

All I want to do is set the 5th column of each line numbered 1-whatever as it's own variable (which will later be used to do some comparisons).

Thanks.
# 2  
Old 06-18-2009
Well, you're using bash, and that has support for arrays (although older versions might not). Second, you can create variables using eval:
Code:
let x=10
eval x_$x=12
set|grep ^x

# 3  
Old 06-18-2009
Quote:
Originally Posted by otheus
Well, you're using bash, and that has support for arrays (although older versions might not). Second, you can create variables using eval:
Code:
let x=10
eval x_$x=12
set|grep ^x

I appreciate the thought. I understand the idea of arrays, but I'm not really at the point where I can use them.

I'm trying to understand the eval command, but I'm not sure I'm getting it. I read through the man page and some examples on line...but it's not sinking in. But I've been up all night doing a maintenance, so I doubt I could understand much of anything right now.
# 4  
Old 06-19-2009
About eval:

The bash shell process the command line by expanding variables and other "magic". After this is done, the line is now ready for processing by the command:
Code:
eval x_10=12

The eval simply executes the arguments as if those arguments had been the command itself. So bash executes:
Code:
x_10=12

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable available for use outside loop

In the below for loop, I extract a variable $d which is an id that will change each time. The bash executes the problem that I am having is that p (after the done) is the path with the extracted $d. However, I can not use it in subsequent loops as it is not reconized. I have been trying to change... (3 Replies)
Discussion started by: cmccabe
3 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

bash - Variable made of variable

Hello, I am struggling with using variable made using "eval". a=4 eval b$a=20 echo $b$a ??? As shown above, I am trying to call back the variable "bX" assuming I do not know the value of "a". How can I do that? I tried several combinations but nothing worked. Thanks (10 Replies)
Discussion started by: jolecanard
10 Replies

6. 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

7. 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

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