LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-09-2009, 02:53 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
Problem with bash script - variable name within variable name


I need to set and then increment a counter in a bash script, where the counter name contains a variable name.

Here is the relevant part of my script

func_count_fmfm () {
echo "Starting FMFM counts for ${category}"
fmfm_umbillingnumber_count_${category}=0

echo ${fmfm_umbillingnumber_count_${category}}
...
}

##################################################################################
# Main routine

...
category="biz"
func_count_fmfm

category="res"
func_count_fmfm
...

The results:
Starting FMFM counts for biz
Starting FMFM counts for res

line 39: fmfm_umbillingnumber_count_biz=0: command not found
line 40: ${fmfm_umbillingnumber_count_${category}}: bad substitution
line 39: fmfm_umbillingnumber_count_res=0: command not found
line 40: ${fmfm_umbillingnumber_count_${category}}: bad substitution

How do I correctly handle a case like this, of a variable name within a variable name?

Thanks in advance...
 
Old 03-09-2009, 04:32 PM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
I don't think that's possible in any programming language. It sounds like you want an array - take a look here or on the bash manpage.
 
0 members found this post helpful.
Old 03-09-2009, 04:45 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can do that by means of "indirect variable reference". Use the notation ${!variable} introduced in Bash version 2:
Code:
#!/bin/bash
func_count_fmfm () {
echo "Starting FMFM counts for ${category}"
eval "fmfm_umbillingnumber_count_${!category}=0"
echo $fmfm_umbillingnumber_count_${!category}
}

category="biz"
func_count_fmfm
category="res"
func_count_fmfm
The eval is mandatory to let the shell do the correct substitution.

Last edited by colucix; 03-09-2009 at 06:36 PM. Reason: Bad grammatical (english) error corrected....
 
1 members found this post helpful.
Old 03-10-2009, 03:08 AM   #4
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Problem with bash script - variable name within variable name

The second post - to use indirect variable reference - worked perfectly! Many thanks...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash/Shell: Problem using a variable in a script redvivi Linux - General 8 08-21-2008 02:02 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
Bash Script Variable Problem jimmy512 Programming 5 05-11-2007 07:38 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM


All times are GMT -5. The time now is 05:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration