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
BASH get variable according to name of another variable test_test Shell Programming and Scripting 3 01-01-2009 05:23 PM
bash and ksh: variable lost in loop in bash? estienne Shell Programming and Scripting 2 08-25-2008 03:09 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
BASH -- sed -- variable Imajean Shell Programming and Scripting 1 04-24-2008 02:14 AM
bash env variable containing @ Yogesh Sawant Shell Programming and Scripting 4 12-20-2006 06:51 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 06-18-2009
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
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 (permalink)  
Old 06-18-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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 (permalink)  
Old 06-18-2009
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
Quote:
Originally Posted by otheus View Post
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 (permalink)  
Old 06-19-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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
Reply

Bookmarks

Tags
eval, variable variable names

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 08:59 PM.


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