LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2010, 11:18 AM   #1
soulcurry
LQ Newbie
 
Registered: Jan 2010
Posts: 1

Rep: Reputation: Disabled
bash scripting - arrays and indirect referencing


Hi,

I've got a situation. I'm having GNU bash version 3.00.16(1) on Solaris 10. I need to declare an array say arr1 which will be populated by an output of a command.

declare -a arr1
arr1=( $(/some/command) )

Supposing it will eventually (after executing the command) have element values as -

arr1[0]=1234
arr1[1]=5678
arr1[2]=7890

Now, I need to declare another set of arrays, one for each of the element values above - e.g.

declare -a arr1_1234
declare -a arr1_5678
declare -a arr1_7890

And I also need to populate elements of each of above 3 arrays with output of another command in a loop. So, these arrays will hold values something like -

arr1_1234[0]="abc"
arr1_1234[1]="def"
arr1_1234[2]="ghi"

arr1_5678[0]="jkl"
arr1_5678[1]="mno"
arr1_5678[2]="pqr"

arr1_7890[0]="tuv"
arr1_7890[1]="xyz"
arr1_7890[2]="aab"


I'm able to declare and populate arr1[*]. My question is how do I declare, populate and print the subsequent arrays and their elements?
I am feeling rather thick to get this working. Any help appreciated.
 
Old 02-23-2010, 01:28 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
You may want to have a look at: http://tldp.org/LDP/abs/html/ivr.html
 
Old 02-23-2010, 02:31 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
The best answer is "Use a different language". Doing something that needs data structures that complicated is generally a Bad Idea in bash. I recommend Python, Ruby, or Perl.
 
Old 02-23-2010, 07:43 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by soulcurry View Post
I'm able to declare and populate arr1[*]. My question is how do I declare, populate and print the subsequent arrays and their elements?
I am feeling rather thick to get this working. Any help appreciated.
Solaris 10 has nawk (and Perl) by default, try to use their associative arrays. I suspect what you are doing could be simpler. describe, if possible, what you are trying to do actually. ( ie moving/copying files? manipulating files ?? )
 
Old 02-23-2010, 09:56 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Concur with tuxdev & ghostdog74; use Perl (or some other lang more powerful than bash).
Tell us what you want to achieve.
 
Old 04-20-2011, 04:09 AM   #6
Enzkha
LQ Newbie
 
Registered: Aug 2005
Location: Finland
Distribution: Debian GNU/Linux, Solaris 10, Centos 4, Centos 5
Posts: 1

Rep: Reputation: 0
I have pondering with the same problem on Solaris 10. If I run declare (on bash) via ssh shell, it works fine. But running it trough script doesn't find the "declare" command. Even though the shell is also bash.

-bash-3.00$ /bin/bash -version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)

declare -a JJ; JJ[0]="Testing"; echo "res -> ${JJ[0]}"
res -> Testing


But then running script remotely via ssh, the env or shell is somehow wrong:

on the script:

declare -a JAVATTI || echo "Debug: SHELL -> `echo $SHELL` "
CCC="0"
echo "Debug: ccc -> ${CCC}, JAVATTI -> ${JAVATTI}"

As a result I get:

Debug: vars: JLK -> /usr/java/bin/java ja LL -> 1
sh: declare: not found
Debug: SHELL -> /bin/bash
Debug: ccc -> 0, JAVATTI ->

I just dont get it. The env should be similar, shell is bash. Why it just doesnt find the builtin functions of bash?
 
Old 04-20-2011, 01:43 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Code:
sh: declare: not found
sh is NOT bash! "declare" is a bash feature that is not in the POSIX sh standard, and when bash is run using the name "sh", some of these features are turned off. I believe the actual problem is that you do not have a proper shebang line, that is, the first line of your script should be
Code:
#!/usr/bin/env bash
(env does a PATH lookup on bash because the exact location of the bash binary can be different depending on the system)

Code:
echo "Debug: SHELL -> `echo $SHELL` "
The use of backticks here is completely pointless. Just do
Code:
echo "Debug: SHELL -> ${SHELL} "
It's unrelated, but do not use backticks. Instead, use $(). Backticks are extremely difficult to nest.
 
Old 04-21-2011, 06:41 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@soulcurry: looks like you're talking about two-dimensional arrays.. if you can upgrade to 4.0+ (I recommend 4.2), you can use associative arrays and run things like:
Code:
arr1["1234,0"]="abc"
arr1["1234,1"]="def"
 
  


Reply



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
Scripting & Arrays : How to fill an array with filenames which consist of spaces? klss Linux - Software 5 01-19-2009 12:16 AM
BASH variable referencing SwingingSimian Programming 3 10-16-2008 05:20 AM
Indirect addressing in bash monz Programming 2 11-30-2006 11:58 AM
Bash indirect reference to array variable(s) sixerjman Programming 6 10-25-2006 11:18 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration