LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-12-2008, 02:42 PM   #1
SlackerJack
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware 11.0
Posts: 28

Rep: Reputation: 15
my var in bash doesn't work: ARRAY_$a_$b="test"


Hi,

I'm trying to kind of emulate an array by getting this to work:

Code:
for MONTH in jan feb mar
do
  for DAY in 1 2 3
  do
      
    # numbers work fine
    let ARRAY_$MONTH_$DAY=$DAY
    
    # strings don't work <<<<<<<<<<<<<<<<<
    #ARRAY_$MONTH_$DAY="test"
    #set ARRAY_$MONTH_$DAY="test"
    #ARRAY_${MONTH}_${DAY}="test"
    #set ARRAY_${MONTH}_${DAY}="test"
    # help please
    
    # print contents
    eval echo \$ARRAY_$MONTH_$DAY
    
  done
done
So, do you know how to get this to work: ARRAY_$MONTH_$DAY="test"

Thanks
 
Old 01-12-2008, 04:58 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
You may find very useful to use the -x option of bash for debugging: if you'd run the script as in
Code:
bash -x script.sh
you would be aware that it doesn't work in the way you expected (neither with numbers nor with strings). To assign variables (without the let statement) you can't use other variables to build the name in the left-hand side of the assignment, otherwise the entire line is interpreted as a command. If you really want to build the variable name using the value of any other variable, you have to consider the eval statement, as you have done for the "print section". For example:
Code:
my_var=hello
eval ${my_var}_world=75      # hello_world=75
echo $hello_world            # 75
Then be careful when you put together (concatenate) variables and strings: the statement
Code:
let ARRAY_$MONTH_$DAY=$DAY
has the string "ARRAY_", the value of a variable $MONTH_ (undefined) and the value of the variable $DAY. In this case the syntax $MONTH cannot be used: the most correct syntax ${MONTH} avoids errors. In summary, a correct version of the script could be
Code:
for MONTH in jan feb mar
do
  for DAY in 1 2 3
  do  
    eval ARRAY_${MONTH}_${DAY}=${DAY}
    eval echo \$ARRAY_${MONTH}_${DAY}
    
    eval ARRAY_${MONTH}_${DAY}="test"
    eval echo \$ARRAY_${MONTH}_${DAY}
    
  done
done
where the brackets in red are mandatory! Also, when you have the basics clear, you may consider to use arrays in bash... without trying to emulate them!
 
Old 01-12-2008, 06:40 PM   #3
SlackerJack
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware 11.0
Posts: 28

Original Poster
Rep: Reputation: 15
Thank you for a lengthy and (very!) informative answer.

Your answer obviously solved my problems.

So close, yet so far away :-)

Quote:
You may find very useful to use the -x option of bash for debugging
Nice, that will come in handy.

Quote:
consider to use arrays in bash
Bash can't handle two-dimentional/multi-dimentional arrays, can it? That's why I had to do something of my own.

Again, great reply...
 
Old 01-13-2008, 03:45 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by SlackerJack View Post
Thank you for a lengthy and (very!) informative answer.
You're welcome!
Quote:
Bash can't handle two-dimentional/multi-dimentional arrays, can it?
It can't indeed. You should consider other languages like perl or gawk, even if they manage multi-dimensional arrays with artifacts. For example, here's an excerpt from "GAWK: Effective AWK Programming" guide:
Quote:
Multidimensional arrays are supported in awk through concatenation of indices into one string. awk converts the indices into strings (see Section 5.4 [Conversion of Strings and Numbers], page 77) and concatenates them together, with a separator between them. This creates a single string that describes the values of the separate indices. The combined string is used as a single index into an ordinary, one-dimensional array. The separator used is the value of the built-in variable SUBSEP.
that sounds very similar to what you're trying to accomplish in bash. Obviously before spending time to learn some other language, you have to evaluate if it's worth to do, depending on your prime objectives.

One thing I forgot to mention in my previous post: it is correct to use the let statement to assign a variable. Indeed it evaluates the expression and let build the name of the variables from other names or let arithmetic expansion like
Code:
my_var=8
let my_var=my_var+10
echo $my_var          # 18

another_var=68
let my_var="75 - $another_var"
echo $my_var          # 7 and so on...
Ciao and good luck!
 
  


Reply

Tags
array, bash, script, variable


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LFS 6.2 Ch. 6.14 - 2nd Coreutils test returns "/bin.bash: No such file or directory" 0graham0 Linux From Scratch 2 08-23-2007 06:32 PM
c gurus: whats the diff? "var[]" and "*var"? kourama Programming 16 04-03-2007 10:28 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM


All times are GMT -5. The time now is 08:37 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