ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 -
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.
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.
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 ?? )
Distribution: Debian GNU/Linux, Solaris 10, Centos 4, Centos 5
Posts: 1
Rep:
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:
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.
@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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.