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.
can i use the value of one variable to generate a name for another variable?
for example i want to use the counter from a "do while" loop to name and define a variable each time the loop executes. for example
objectnames1=`ls -a`
objectnames2=`ls -a`
etc.
i don't have a script yet but each time through the loop i intend to cd to a particular directory and then define a variable containing a list of each object in that directory as values. for the rest of the script to work, each variable generated has to be unique, and i can't think of a good way to accomplish this.
if using a value from one variable to name another isn't possible, can anyone think of a more elegant solution? i know limited syntax but i'm willing to read up...
I'd probably use arrays. But if you have to use them intensively I'd better use another language, like python or perl. That is, unless you have no other option.
bash has basic arrays for you. If you have bash 4.0, you can use associative arrays.
For your case
Code:
#!/bin/bash
declare -a array
array=( $(ls -a) )
echo ${array[*]}
echo ${array[0]}
echo ${array[1]}
echo ${array[2]} #and so on
unless you are going to use these directory listing in later part of your script, otherwise, if its only a one time event, just use a for loop to go over your files (or using find etc ). No need to store them into arrays
Code:
#!/bin/bash
shopt -s nullglob
for file in *
do
# do your stuff
done
Last edited by ghostdog74; 07-26-2010 at 04:43 AM.
I agree arrays are probably what you need, but as per i92guboj I'd use Perl instead, judging by the brief description of your problem, and in that case, possibly hashes.
Depends exactly what the rest your requirement is...
this being my first time shell scripting for any purpose, i didn't even know that bash had support for arrays. thanx everyone for pointing me in the right direction. did some reading... learned the syntax... problem solved.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.