Linux - NewbieThis 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
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.
Okay, well to help you, we're going to need you to answer a few questions.
1. What programming language are you using?
2. What format are these arrays in?
Thank you for your quick response. To answer your questions, I am programming in BASH and the format of the arrays are as follows: arrayname=(arrayelements)
Last edited by nobody123; 02-19-2009 at 04:06 PM.
Reason: forgot an '=' sign
Okay, so we have the name, followed by the list in parentheses. Are these elements separated by commas, tabs, spaces?
In bash, most array handling is kinda ad-hoc. By that I mean that it depends a lot on what you want to do. Bash doesn't really have built in arrays. So do you want to iterate over each element and execute some command? Or do you want to add them up? Or something else?
Okay, so we have the name, followed by the list in parentheses. Are these elements separated by commas, tabs, spaces?
In bash, most array handling is kinda ad-hoc. By that I mean that it depends a lot on what you want to do. Bash doesn't really have built in arrays. So do you want to iterate over each element and execute some command? Or do you want to add them up? Or something else?
The elements are indeed separated by spaces. What I want to do with the array is read the array elements and execute a command (e.g. using the 'grep' command to search for a specific value and display).
for X in `sed name_of_the_file -r -e 's/.*\((.*)\)/\1/g' | tr -d ','`
do
#Your command here, where $X is replaced with this entry in the array
done
This takes the file and the sed command strips off the name of the array and the parentheses. The output of that is then sent (because of the |) to tr -d ',' which pulls off all the commas, so that now you just have numbers separated by spaces.
the
for [variable name] in [series of values separated by spaces]
do
#stuff
done
for X in `sed name_of_the_file -r -e 's/.*\((.*)\)/\1/g' | tr -d ','`
do
#Your command here, where $X is replaced with this entry in the array
done
This takes the file and the sed command strips off the name of the array and the parentheses. The output of that is then sent (because of the |) to tr -d ',' which pulls off all the commas, so that now you just have numbers separated by spaces.
the
for [variable name] in [series of values separated by spaces]
do
#stuff
done
is the bash for-each loop.
Thank you for your piece of code. There are a few questions I have about it though:
Is it really necessary to have commas in an array? I have not been using any. If not, it is necessary to have | tr -d ','?
"Where X is replaced with this entry in the array"...does that mean an array element is supposed to be in 'X'?
Here is how I have been creating my arrays as of late:
Haha, no that is my mistake. I forgot that you said they were separated by spaces, I thought they were separated by commas. So yes - if you leave out the spaces, you can also leave out the tr -d ','.
Now I didn't realize that the elements of the arrays have spaces within them. That breaks the for - loop because it will try to call on things like '"The', 'Beatles"', '"Elvis', 'Presley"' and so on. Hrm, we'll need a new approach.
Haha, no that is my mistake. I forgot that you said they were separated by spaces, I thought they were separated by commas. So yes - if you leave out the spaces, you can also leave out the tr -d ','.
Now I didn't realize that the elements of the arrays have spaces within them. That breaks the for - loop because it will try to call on things like '"The', 'Beatles"', '"Elvis', 'Presley"' and so on. Hrm, we'll need a new approach.
I'll think about this, and get back.
Thanks again for your help. Please take your time!
Do you put multiple arrays in one file? The whole problem gets much easier if the entries are one per line.
Yes, I do put multiple arrays in one file. I do believe I have been putting the arrays one per line (I do have more than one array. Here they are if you are interested in addition to the previous array stated:
If that is the actual form of the file you saved, the lines are also legal bash statements, and you can simply source it.
You are using a strange double quote. If you convert them to simple double quotes `"', then simply run:
. yourfile
After that, you can access the arrays normally.
. tempfile.sh
jschiwal@hpmedia:~> echo ${CDName[0]}
Please Please Me
jschiwal@hpmedia:~> echo ${CDName[1]}
Loving You
Here is an example where I used sed to use the NAME=VALUES results from ogginfo. I eval the results to set variables for a ogg2mp3 script.
Code:
cat bin/get_tags
get_tags()
{
# clear out old tags
ALBUM='';
ARTIST='';
DATE='';
GENRE='';
TITLE='';
# the tags returned by ogginfo are of the form NAME=VALUE.
# put single quotes around the values to encapsulate white space.
# eval will set the tag variables ALBUM, ARTIST, DATE, GENRE, TITLE
eval $(ogginfo ${1} | sed '/[A-Z][A-Z]*=/!d;s/=\(.*\)$/='"'"'\1'"'"'/;s/^\t//')
}
If that is the actual form of the file you saved, the lines are also legal bash statements, and you can simply source it.
You are using a strange double quote. If you convert them to simple double quotes `"', then simply run:
. yourfile
After that, you can access the arrays normally.
. tempfile.sh
jschiwal@hpmedia:~> echo ${CDName[0]}
Please Please Me
jschiwal@hpmedia:~> echo ${CDName[1]}
Loving You
Here is an example where I used sed to use the NAME=VALUES results from ogginfo. I eval the results to set variables for a ogg2mp3 script.
Code:
cat bin/get_tags
get_tags()
{
# clear out old tags
ALBUM='';
ARTIST='';
DATE='';
GENRE='';
TITLE='';
# the tags returned by ogginfo are of the form NAME=VALUE.
# put single quotes around the values to encapsulate white space.
# eval will set the tag variables ALBUM, ARTIST, DATE, GENRE, TITLE
eval $(ogginfo ${1} | sed '/[A-Z][A-Z]*=/!d;s/=\(.*\)$/='"'"'\1'"'"'/;s/^\t//')
}
Thank you jschiwal for your lovely piece of code. The reasons why I had those strange double quotes is because I thought of simulating strings. Then I realized that it is already in a string so there is actually no need . Anyways, I'll work with your code and get back here when I finally get the script to work the way I want it to. Thank you again!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.